bitstreamreader.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. //--=========================================================================--
  3. // This file is a part of VPU Reference API project
  4. //-----------------------------------------------------------------------------
  5. // This confidential and proprietary software may be used only
  6. // as authorized by a licensing agreement from Chips&Media Inc.
  7. // In the event of publication, the following notice is applicable:
  8. //
  9. // (C) COPYRIGHT 2006 - 2013 CHIPS&MEDIA INC.
  10. // ALL RIGHTS RESERVED
  11. //
  12. // The entire notice above must be reproduced on all authorized
  13. // copies.
  14. //
  15. //--=========================================================================--
  16. #include "main_helper.h"
  17. typedef struct {
  18. Uint32 type;
  19. EndianMode endian;
  20. BitstreamReaderImpl* impl;
  21. osal_file_t* fp;
  22. EncHandle* handle;
  23. } AbstractBitstreamReader;
  24. BitstreamReader BitstreamReader_Create(
  25. Uint32 type,
  26. char* path,
  27. EndianMode endian,
  28. EncHandle* handle
  29. )
  30. {
  31. AbstractBitstreamReader* reader;
  32. osal_file_t *fp;
  33. if ( path == NULL) {
  34. VLOG(ERR, "%s:%d path is NULL\n", __FUNCTION__, __LINE__);
  35. return NULL;
  36. }
  37. if ((fp=osal_fopen(path, "wb")) == NULL) {
  38. VLOG(ERR, "%s:%d failed to open bin file: %s\n", __FUNCTION__, __LINE__, path);
  39. return FALSE;
  40. }
  41. VLOG(INFO, "output bin file: %s\n", path);
  42. reader = (AbstractBitstreamReader*)osal_malloc(sizeof(AbstractBitstreamReader));
  43. reader->fp = fp;
  44. reader->handle = handle;
  45. reader->type = type;
  46. reader->endian = endian;
  47. return reader;
  48. }
  49. BOOL BitstreamReader_Act(
  50. BitstreamReader reader,
  51. PhysicalAddress bitstreamBuffer,
  52. Uint32 bitstreamBufferSize,
  53. Uint32 streamReadSize,
  54. Comparator comparator
  55. )
  56. {
  57. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  58. osal_file_t *fp;
  59. EncHandle *handle;
  60. RetCode ret = RETCODE_SUCCESS;
  61. PhysicalAddress paRdPtr;
  62. PhysicalAddress paWrPtr;
  63. int size = 0;
  64. Int32 loadSize = 0;
  65. PhysicalAddress paBsBufStart = bitstreamBuffer;
  66. PhysicalAddress paBsBufEnd = bitstreamBuffer+bitstreamBufferSize;
  67. Uint8* buf = NULL;
  68. Uint32 coreIdx;
  69. BOOL success = TRUE;
  70. if (reader == NULL) {
  71. #ifdef SUPPORT_DONT_READ_STREAM
  72. return TRUE;
  73. #else
  74. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  75. return FALSE;
  76. #endif
  77. }
  78. fp = absReader->fp;
  79. handle = absReader->handle;
  80. coreIdx = VPU_HANDLE_CORE_INDEX(*handle);
  81. ret = VPU_EncGetBitstreamBuffer(*handle, &paRdPtr, &paWrPtr, &size);
  82. if (size > 0) {
  83. if (streamReadSize > 0) {
  84. if ((Uint32)size < streamReadSize) {
  85. loadSize = size;
  86. }
  87. else {
  88. loadSize = streamReadSize;
  89. }
  90. }
  91. else {
  92. loadSize = size;
  93. }
  94. buf = (Uint8*)osal_malloc(loadSize);
  95. if (buf == NULL) {
  96. return FALSE;
  97. }
  98. if (absReader->type == BUFFER_MODE_TYPE_RINGBUFFER) {
  99. if ((paRdPtr+loadSize) > paBsBufEnd) {
  100. Uint32 room;
  101. room = paBsBufEnd - paRdPtr;
  102. vdi_read_memory(coreIdx, paRdPtr, buf, room, absReader->endian);
  103. vdi_read_memory(coreIdx, paBsBufStart, buf+room, (loadSize-room), absReader->endian);
  104. }
  105. else {
  106. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  107. }
  108. }
  109. else {
  110. /* Linebuffer */
  111. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  112. }
  113. if (fp != NULL) {
  114. osal_fwrite((void *)buf, sizeof(Uint8), loadSize, fp);
  115. osal_fflush(fp);
  116. }
  117. if (comparator != NULL) {
  118. if (Comparator_Act(comparator, buf, loadSize) == FALSE) {
  119. success = FALSE;
  120. }
  121. }
  122. osal_free(buf);
  123. ret = VPU_EncUpdateBitstreamBuffer(*handle, loadSize);
  124. if( ret != RETCODE_SUCCESS ) {
  125. VLOG(ERR, "VPU_EncUpdateBitstreamBuffer failed Error code is 0x%x \n", ret );
  126. success = FALSE;
  127. }
  128. }
  129. return success;
  130. }
  131. BOOL BitstreamReader_Destroy(
  132. BitstreamReader reader
  133. )
  134. {
  135. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  136. if (reader == NULL) {
  137. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  138. return FALSE;
  139. }
  140. osal_fclose(absReader->fp);
  141. osal_free(absReader);
  142. return TRUE;
  143. }
  144. BitstreamReader BufferStreamReader_Create(
  145. Uint32 type,
  146. EndianMode endian,
  147. EncHandle* handle
  148. )
  149. {
  150. AbstractBitstreamReader* reader;
  151. reader = (AbstractBitstreamReader*)osal_malloc(sizeof(AbstractBitstreamReader));
  152. reader->handle = handle;
  153. reader->type = type;
  154. reader->endian = endian;
  155. return reader;
  156. }
  157. Uint32 BufferStreamReader_Act(
  158. BitstreamReader reader,
  159. PhysicalAddress bitstreamBuffer,
  160. Uint32 bitstreamBufferSize,
  161. Uint32 streamReadSize,
  162. Uint8* pBuffer,
  163. Comparator comparator
  164. )
  165. {
  166. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  167. EncHandle *handle;
  168. RetCode ret = RETCODE_SUCCESS;
  169. PhysicalAddress paRdPtr;
  170. PhysicalAddress paWrPtr;
  171. int size = 0;
  172. Int32 loadSize = 0;
  173. PhysicalAddress paBsBufStart = bitstreamBuffer;
  174. PhysicalAddress paBsBufEnd = bitstreamBuffer+bitstreamBufferSize;
  175. Uint32 coreIdx;
  176. if (reader == NULL) {
  177. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  178. return 0;
  179. }
  180. handle = absReader->handle;
  181. coreIdx = VPU_HANDLE_CORE_INDEX(*handle);
  182. ret = VPU_EncGetBitstreamBuffer(*handle, &paRdPtr, &paWrPtr, &size);
  183. if (size > 0) {
  184. if (streamReadSize > 0) {
  185. if ((Uint32)size < streamReadSize) {
  186. loadSize = size;
  187. }
  188. else {
  189. loadSize = streamReadSize;
  190. }
  191. }
  192. else {
  193. loadSize = size;
  194. }
  195. if (absReader->type == BUFFER_MODE_TYPE_RINGBUFFER) {
  196. if ((paRdPtr+loadSize) > paBsBufEnd) {
  197. Uint32 room;
  198. room = paBsBufEnd - paRdPtr;
  199. vdi_read_memory(coreIdx, paRdPtr, pBuffer, room, absReader->endian);
  200. vdi_read_memory(coreIdx, paBsBufStart, pBuffer+room, (loadSize-room), absReader->endian);
  201. }
  202. else {
  203. vdi_read_memory(coreIdx, paRdPtr, pBuffer, loadSize, absReader->endian);
  204. }
  205. }
  206. else {
  207. /* Linebuffer */
  208. vdi_read_memory(coreIdx, paRdPtr, pBuffer, loadSize, absReader->endian);
  209. }
  210. ret = VPU_EncUpdateBitstreamBuffer(*handle, loadSize);
  211. if( ret != RETCODE_SUCCESS ) {
  212. VLOG(ERR, "VPU_EncUpdateBitstreamBuffer failed Error code is 0x%x \n", ret );
  213. return 0;
  214. }
  215. }
  216. return loadSize;
  217. }
  218. BOOL BufferStreamReader_Destroy(
  219. BitstreamReader reader
  220. )
  221. {
  222. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  223. if (reader == NULL) {
  224. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  225. return FALSE;
  226. }
  227. osal_free(absReader);
  228. return TRUE;
  229. }