bitstreamreader.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 "main_helper.h"
  26. typedef struct {
  27. Uint32 type;
  28. EndianMode endian;
  29. BitstreamReaderImpl* impl;
  30. osal_file_t* fp;
  31. EncHandle* handle;
  32. } AbstractBitstreamReader;
  33. BitstreamReader BitstreamReader_Create(
  34. Uint32 type,
  35. char* path,
  36. EndianMode endian,
  37. EncHandle* handle
  38. )
  39. {
  40. AbstractBitstreamReader* reader;
  41. osal_file_t *fp=NULL;
  42. #ifdef USE_FEEDING_METHOD_BUFFER
  43. #else
  44. if ( path[0] != 0) {
  45. if ((fp=osal_fopen(path, "wb")) == NULL) {
  46. VLOG(ERR, "%s:%d failed to open bin file: %s\n", __FUNCTION__, __LINE__, path);
  47. return FALSE;
  48. }
  49. VLOG(INFO, "output bin file: %s\n", path);
  50. }
  51. else
  52. VLOG(ERR, "%s:%d Bitstream File path is NULL : no save\n", __FUNCTION__, __LINE__);
  53. #endif
  54. reader = (AbstractBitstreamReader*)osal_malloc(sizeof(AbstractBitstreamReader));
  55. reader->fp = fp;
  56. reader->handle = handle;
  57. reader->type = type;
  58. reader->endian = endian;
  59. return reader;
  60. }
  61. BOOL BitstreamReader_Act(
  62. BitstreamReader reader,
  63. PhysicalAddress bitstreamBuffer,
  64. Uint32 bitstreamBufferSize,
  65. Uint32 streamReadSize,
  66. Comparator comparator
  67. )
  68. {
  69. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  70. osal_file_t *fp;
  71. EncHandle *handle;
  72. RetCode ret = RETCODE_SUCCESS;
  73. PhysicalAddress paRdPtr;
  74. PhysicalAddress paWrPtr;
  75. int size = 0;
  76. Int32 loadSize = 0;
  77. PhysicalAddress paBsBufStart = bitstreamBuffer;
  78. PhysicalAddress paBsBufEnd = bitstreamBuffer+bitstreamBufferSize;
  79. Uint8* buf = NULL;
  80. Uint32 coreIdx;
  81. BOOL success = TRUE;
  82. if (reader == NULL) {
  83. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  84. return FALSE;
  85. }
  86. if (streamReadSize == 0) {
  87. return TRUE;
  88. }
  89. fp = absReader->fp;
  90. handle = absReader->handle;
  91. coreIdx = VPU_HANDLE_CORE_INDEX(*handle);
  92. ret = VPU_EncGetBitstreamBuffer(*handle, &paRdPtr, &paWrPtr, &size);
  93. if (size > 0) {
  94. if (streamReadSize > 0) {
  95. if ((Uint32)size < streamReadSize) {
  96. loadSize = size;
  97. }
  98. else {
  99. loadSize = streamReadSize;
  100. }
  101. }
  102. else {
  103. loadSize = size;
  104. }
  105. buf = (Uint8*)osal_malloc(loadSize);
  106. if (buf == NULL) {
  107. return FALSE;
  108. }
  109. if (absReader->type == BUFFER_MODE_TYPE_RINGBUFFER) {
  110. if ((paRdPtr+loadSize) > paBsBufEnd) {
  111. Uint32 room = paBsBufEnd - paRdPtr;
  112. vdi_read_memory(coreIdx, paRdPtr, buf, room, absReader->endian);
  113. vdi_read_memory(coreIdx, paBsBufStart, buf+room, (loadSize-room), absReader->endian);
  114. }
  115. else {
  116. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  117. }
  118. }
  119. else {
  120. /* Linebuffer */
  121. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  122. }
  123. if (fp != NULL) {
  124. osal_fwrite((void *)buf, sizeof(Uint8), loadSize, fp);
  125. osal_fflush(fp);
  126. }
  127. if (comparator != NULL) {
  128. if (Comparator_Act(comparator, buf, loadSize) == FALSE) {
  129. success = FALSE;
  130. }
  131. }
  132. osal_free(buf);
  133. ret = VPU_EncUpdateBitstreamBuffer(*handle, loadSize);
  134. if( ret != RETCODE_SUCCESS ) {
  135. VLOG(ERR, "VPU_EncUpdateBitstreamBuffer failed Error code is 0x%x \n", ret );
  136. success = FALSE;
  137. }
  138. }
  139. return success;
  140. }
  141. Uint32 BitstreamReader_Act2(
  142. BitstreamReader reader,
  143. PhysicalAddress bitstreamBuffer,
  144. Uint32 bitstreamBufferSize,
  145. Uint32 streamReadSize,
  146. Comparator comparator,
  147. Uint8* buf
  148. )
  149. {
  150. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  151. EncHandle *handle;
  152. RetCode ret = RETCODE_SUCCESS;
  153. PhysicalAddress paRdPtr;
  154. PhysicalAddress paWrPtr;
  155. int size = 0;
  156. Int32 loadSize = 0;
  157. PhysicalAddress paBsBufStart = bitstreamBuffer;
  158. PhysicalAddress paBsBufEnd = bitstreamBuffer+bitstreamBufferSize;
  159. Uint32 coreIdx;
  160. if (reader == NULL) {
  161. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  162. return 0;
  163. }
  164. if (streamReadSize == 0) {
  165. return 0;
  166. }
  167. if (buf == NULL) {
  168. VLOG(ERR, "%s:%d Invalid buf address\n", __FUNCTION__, __LINE__);
  169. return 0;
  170. }
  171. handle = absReader->handle;
  172. coreIdx = VPU_HANDLE_CORE_INDEX(*handle);
  173. ret = VPU_EncGetBitstreamBuffer(*handle, &paRdPtr, &paWrPtr, &size);
  174. if (size > 0) {
  175. if (streamReadSize > 0) {
  176. if ((Uint32)size < streamReadSize) {
  177. loadSize = size;
  178. }
  179. else {
  180. loadSize = streamReadSize;
  181. }
  182. }
  183. else {
  184. loadSize = size;
  185. }
  186. if (absReader->type == BUFFER_MODE_TYPE_RINGBUFFER) {
  187. if ((paRdPtr+loadSize) > paBsBufEnd) {
  188. Uint32 room = paBsBufEnd - paRdPtr;
  189. vdi_read_memory(coreIdx, paRdPtr, buf, room, absReader->endian);
  190. vdi_read_memory(coreIdx, paBsBufStart, buf+room, (loadSize-room), absReader->endian);
  191. }
  192. else {
  193. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  194. }
  195. }
  196. else {
  197. /* Linebuffer */
  198. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  199. }
  200. if (comparator != NULL) {
  201. if (Comparator_Act(comparator, buf, loadSize) == FALSE) {
  202. loadSize = 0;
  203. }
  204. }
  205. ret = VPU_EncUpdateBitstreamBuffer(*handle, loadSize);
  206. if( ret != RETCODE_SUCCESS ) {
  207. VLOG(ERR, "VPU_EncUpdateBitstreamBuffer failed Error code is 0x%x \n", ret );
  208. loadSize = 0;
  209. }
  210. }
  211. return loadSize;
  212. }
  213. BOOL BitstreamReader_Destroy(
  214. BitstreamReader reader
  215. )
  216. {
  217. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  218. if (absReader == NULL) {
  219. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  220. return FALSE;
  221. }
  222. osal_fclose(absReader->fp);
  223. osal_free(absReader);
  224. return TRUE;
  225. }