bitstreamreader.c 7.4 KB

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