component_enc_reader.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 <string.h>
  26. #include "component.h"
  27. typedef enum {
  28. READER_STATE_OPEN,
  29. READER_STATE_READING,
  30. } EncReaderState;
  31. typedef struct {
  32. EncHandle handle;
  33. Uint32 streamBufCount;
  34. Uint32 streamBufSize;
  35. vpu_buffer_t* bsBuffer;
  36. Uint32 coreIdx;
  37. EndianMode streamEndian;
  38. EncReaderState state;
  39. char bitstreamFileName[MAX_FILE_PATH];
  40. BitstreamReader bsReader;
  41. osal_file_t fp;
  42. BOOL ringBuffer;
  43. BOOL ringBufferWrapEnable;
  44. Int32 productID;
  45. } ReaderContext;
  46. static CNMComponentParamRet GetParameterReader(ComponentImpl* from, ComponentImpl* com, GetParameterCMD commandType, void* data)
  47. {
  48. ReaderContext* ctx = (ReaderContext*)com->context;
  49. BOOL result = TRUE;
  50. ParamEncBitstreamBuffer* bsBuf = NULL;
  51. PortContainer* container;
  52. switch(commandType) {
  53. case GET_PARAM_COM_IS_CONTAINER_CONUSUMED:
  54. container = (PortContainer*)data;
  55. container->consumed = TRUE;
  56. break;
  57. case GET_PARAM_READER_BITSTREAM_BUF:
  58. if (ctx->bsBuffer == NULL) return CNM_COMPONENT_PARAM_NOT_READY;
  59. bsBuf = (ParamEncBitstreamBuffer*)data;
  60. bsBuf->bs = ctx->bsBuffer;
  61. bsBuf->num = ctx->streamBufCount;
  62. break;
  63. default:
  64. return CNM_COMPONENT_PARAM_NOT_FOUND;
  65. }
  66. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  67. }
  68. static CNMComponentParamRet SetParameterReader(ComponentImpl* from, ComponentImpl* com, SetParameterCMD commandType, void* data)
  69. {
  70. BOOL result = TRUE;
  71. switch(commandType) {
  72. default:
  73. result = FALSE;
  74. break;
  75. }
  76. return (result == TRUE) ? CNM_COMPONENT_PARAM_SUCCESS : CNM_COMPONENT_PARAM_FAILURE;
  77. }
  78. static BOOL ExecuteReader(ComponentImpl* com, PortContainer* in, PortContainer* out)
  79. {
  80. ReaderContext* ctx = (ReaderContext*)com->context;
  81. PortContainerES* srcData = (PortContainerES*)in;
  82. BOOL success = TRUE;
  83. CNMComponentParamRet ret;
  84. srcData->reuse = FALSE;
  85. switch (ctx->state) {
  86. case READER_STATE_OPEN:
  87. ret = ComponentGetParameter(com, com->srcPort.connectedComponent, GET_PARAM_ENC_HANDLE, &ctx->handle);
  88. if (ComponentParamReturnTest(ret, &success) == FALSE) {
  89. return success;
  90. }
  91. if ( ctx->bitstreamFileName[0] != 0) {
  92. ctx->bsReader = BitstreamReader_Create(BUFFER_MODE_TYPE_LINEBUFFER, ctx->bitstreamFileName, ctx->streamEndian, &ctx->handle);
  93. if (ctx->bsReader == NULL) {
  94. VLOG(ERR, "%s:%d Failed to BitstreamReader_Create\n", __FUNCTION__, __LINE__);
  95. return FALSE;
  96. }
  97. }
  98. ctx->state = READER_STATE_READING;
  99. srcData->reuse = TRUE;
  100. break;
  101. case READER_STATE_READING:
  102. if (srcData->size > 0 ||
  103. (ctx->ringBuffer == TRUE && ctx->ringBufferWrapEnable == FALSE && srcData->last == TRUE) ) {
  104. if ( ctx->ringBuffer == TRUE) {
  105. Uint8* buf = (Uint8*)osal_malloc(srcData->size);
  106. PhysicalAddress rd, paBsBufStart, paBsBufEnd;
  107. Int32 readSize;
  108. Uint32 room;
  109. rd = srcData->rdPtr;
  110. paBsBufStart = srcData->paBsBufStart;
  111. paBsBufEnd = srcData->paBsBufEnd;
  112. readSize = srcData->size;
  113. if (ctx->ringBufferWrapEnable == TRUE ) {
  114. if (ctx->bsReader != 0) {
  115. if ((rd+readSize) > paBsBufEnd) {
  116. room = paBsBufEnd - rd;
  117. vdi_read_memory(ctx->coreIdx, rd, buf, room, ctx->streamEndian);
  118. vdi_read_memory(ctx->coreIdx, paBsBufStart, buf+room, (readSize-room), ctx->streamEndian);
  119. }
  120. else {
  121. vdi_read_memory(ctx->coreIdx, rd, buf, readSize, ctx->streamEndian);
  122. }
  123. }
  124. VPU_EncUpdateBitstreamBuffer(ctx->handle, readSize);
  125. if (ctx->fp) {
  126. osal_fwrite(buf, readSize, 1, ctx->fp);
  127. }
  128. }
  129. else { //ring=1, wrap=0
  130. if (srcData->streamBufFull == TRUE || srcData->last == TRUE) { // read whole data at once and no UpdateBS
  131. if (ctx->bsReader != 0) {
  132. vdi_read_memory(ctx->coreIdx, rd, buf, readSize, ctx->streamEndian);
  133. }
  134. if (ctx->fp) {
  135. osal_fwrite(buf, readSize, 1, ctx->fp);
  136. }
  137. }
  138. }
  139. if (srcData->streamBufFull == TRUE) {
  140. if (ctx->ringBufferWrapEnable == FALSE) {
  141. vdi_free_dma_memory(ctx->coreIdx, &srcData->buf, ENC_BS, ctx->handle->instIndex);
  142. osal_memcpy(ctx->bsBuffer, &srcData->newBsBuf, sizeof(*ctx->bsBuffer));
  143. }
  144. srcData->buf.size = 0;
  145. }
  146. osal_free(buf);
  147. }
  148. else { /* line buffer mode */
  149. Uint8* buf = (Uint8*)osal_malloc(srcData->size);
  150. if (ctx->bsReader != 0) {
  151. //buf = (Uint8*)osal_malloc(srcData->size);
  152. BitstreamReader_Act(ctx->bsReader, srcData->buf.phys_addr, srcData->buf.size, srcData->size, NULL);
  153. if(PRODUCT_ID_NOT_W_SERIES(ctx->productID) && TRUE == srcData->streamBufFull) {
  154. VPU_ClearInterrupt(ctx->coreIdx);
  155. }
  156. }
  157. if (srcData->streamBufFull == TRUE) {
  158. srcData->buf.phys_addr = 0;
  159. }
  160. if (ctx->fp) {
  161. osal_fwrite(buf, srcData->size, 1, ctx->fp);
  162. }
  163. if(buf)
  164. osal_free(buf);
  165. }
  166. if (TRUE == srcData->streamBufFull) {
  167. srcData->streamBufFull = FALSE;
  168. }
  169. }
  170. srcData->consumed = TRUE;
  171. com->terminate = srcData->last;
  172. break;
  173. default:
  174. success = FALSE;
  175. break;
  176. }
  177. return success;
  178. }
  179. static BOOL PrepareReader(ComponentImpl* com, BOOL* done)
  180. {
  181. ReaderContext* ctx = (ReaderContext*)com->context;
  182. Uint32 i;
  183. vpu_buffer_t* bsBuffer;
  184. Uint32 num = ctx->streamBufCount;
  185. *done = FALSE;
  186. bsBuffer = (vpu_buffer_t*)osal_malloc(num*sizeof(vpu_buffer_t));
  187. for (i = 0; i < num; i++) {
  188. bsBuffer[i].size = ctx->streamBufSize;
  189. if (vdi_allocate_dma_memory(ctx->coreIdx, &bsBuffer[i], ENC_BS, /*ctx->handle->instIndex*/0) < 0) {
  190. VLOG(ERR, "%s:%d fail to allocate bitstream buffer\n", __FUNCTION__, __LINE__);
  191. osal_free(bsBuffer);
  192. return FALSE;
  193. }
  194. }
  195. ctx->bsBuffer = bsBuffer;
  196. ctx->state = READER_STATE_OPEN;
  197. *done = TRUE;
  198. return TRUE;
  199. }
  200. static void ReleaseReader(ComponentImpl* com)
  201. {
  202. ReaderContext* ctx = (ReaderContext*)com->context;
  203. Uint32 i = 0;
  204. if (ctx->bsBuffer != NULL) {
  205. for (i = 0; i < ctx->streamBufCount ; i++) {
  206. if (ctx->bsBuffer[i].size)
  207. vdi_free_dma_memory(ctx->coreIdx, &ctx->bsBuffer[i], ENC_BS, 0);
  208. }
  209. }
  210. }
  211. static BOOL DestroyReader(ComponentImpl* com)
  212. {
  213. ReaderContext* ctx = (ReaderContext*)com->context;
  214. if (ctx->bsBuffer != NULL) osal_free(ctx->bsBuffer);
  215. if (ctx->fp) osal_fclose(ctx->fp);
  216. osal_free(ctx);
  217. return TRUE;
  218. }
  219. static Component CreateReader(ComponentImpl* com, CNMComponentConfig* componentParam)
  220. {
  221. ReaderContext* ctx;
  222. com->context = osal_malloc(sizeof(ReaderContext));
  223. ctx = (ReaderContext*)com->context;
  224. osal_memset((void*)ctx, 0, sizeof(ReaderContext));
  225. strcpy(ctx->bitstreamFileName, componentParam->testEncConfig.bitstreamFileName);
  226. ctx->handle = NULL;
  227. ctx->coreIdx = componentParam->testEncConfig.coreIdx;
  228. ctx->productID = componentParam->testEncConfig.productId;
  229. ctx->streamEndian = (EndianMode)componentParam->testEncConfig.stream_endian;
  230. ctx->streamBufCount = componentParam->encOpenParam.streamBufCount;
  231. ctx->streamBufSize = componentParam->encOpenParam.streamBufSize;
  232. ctx->ringBuffer = componentParam->encOpenParam.ringBufferEnable;
  233. ctx->ringBufferWrapEnable = componentParam->encOpenParam.ringBufferWrapEnable;
  234. return (Component)com;
  235. }
  236. ComponentImpl readerComponentImpl = {
  237. "reader",
  238. NULL,
  239. {0,},
  240. {0,},
  241. sizeof(PortContainer),
  242. 5,
  243. CreateReader,
  244. GetParameterReader,
  245. SetParameterReader,
  246. PrepareReader,
  247. ExecuteReader,
  248. ReleaseReader,
  249. DestroyReader,
  250. };