bitstreamreader.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. /*
  3. * Copyright (c) 2019, Chips&Media
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "main_helper.h"
  27. typedef struct {
  28. Uint32 type;
  29. EndianMode endian;
  30. BitstreamReaderImpl* impl;
  31. osal_file_t* fp;
  32. EncHandle* handle;
  33. } AbstractBitstreamReader;
  34. BitstreamReader BitstreamReader_Create(
  35. Uint32 type,
  36. char* path,
  37. EndianMode endian,
  38. EncHandle* handle
  39. )
  40. {
  41. AbstractBitstreamReader* reader;
  42. osal_file_t *fp=NULL;
  43. if ( path[0] != 0) {
  44. if ((fp=osal_fopen(path, "wb")) == NULL) {
  45. VLOG(ERR, "%s:%d failed to open bin file: %s\n", __FUNCTION__, __LINE__, path);
  46. return FALSE;
  47. }
  48. VLOG(INFO, "output bin file: %s\n", path);
  49. }
  50. else
  51. VLOG(ERR, "%s:%d Bitstream File path is NULL : no save\n", __FUNCTION__, __LINE__);
  52. reader = (AbstractBitstreamReader*)osal_malloc(sizeof(AbstractBitstreamReader));
  53. reader->fp = fp;
  54. reader->handle = handle;
  55. reader->type = type;
  56. reader->endian = endian;
  57. return reader;
  58. }
  59. BOOL BitstreamReader_Act(
  60. BitstreamReader reader,
  61. PhysicalAddress bitstreamBuffer,
  62. Uint32 bitstreamBufferSize,
  63. Uint32 streamReadSize,
  64. Comparator comparator
  65. )
  66. {
  67. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  68. osal_file_t *fp;
  69. EncHandle *handle;
  70. RetCode ret = RETCODE_SUCCESS;
  71. PhysicalAddress paRdPtr;
  72. PhysicalAddress paWrPtr;
  73. int size = 0;
  74. Int32 loadSize = 0;
  75. PhysicalAddress paBsBufStart = bitstreamBuffer;
  76. PhysicalAddress paBsBufEnd = bitstreamBuffer+bitstreamBufferSize;
  77. Uint8* buf = NULL;
  78. Uint32 coreIdx;
  79. BOOL success = TRUE;
  80. if (reader == NULL) {
  81. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  82. return FALSE;
  83. }
  84. if (streamReadSize == 0) {
  85. return TRUE;
  86. }
  87. fp = absReader->fp;
  88. handle = absReader->handle;
  89. coreIdx = VPU_HANDLE_CORE_INDEX(*handle);
  90. ret = VPU_EncGetBitstreamBuffer(*handle, &paRdPtr, &paWrPtr, &size);
  91. if (size > 0) {
  92. if (streamReadSize > 0) {
  93. if ((Uint32)size < streamReadSize) {
  94. loadSize = size;
  95. }
  96. else {
  97. loadSize = streamReadSize;
  98. }
  99. }
  100. else {
  101. loadSize = size;
  102. }
  103. buf = (Uint8*)osal_malloc(loadSize);
  104. if (buf == NULL) {
  105. return FALSE;
  106. }
  107. if (absReader->type == BUFFER_MODE_TYPE_RINGBUFFER) {
  108. if ((paRdPtr+loadSize) > paBsBufEnd) {
  109. Uint32 room = paBsBufEnd - paRdPtr;
  110. vdi_read_memory(coreIdx, paRdPtr, buf, room, absReader->endian);
  111. vdi_read_memory(coreIdx, paBsBufStart, buf+room, (loadSize-room), absReader->endian);
  112. }
  113. else {
  114. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  115. }
  116. }
  117. else {
  118. /* Linebuffer */
  119. vdi_read_memory(coreIdx, paRdPtr, buf, loadSize, absReader->endian);
  120. }
  121. if (fp != NULL) {
  122. osal_fwrite((void *)buf, sizeof(Uint8), loadSize, fp);
  123. osal_fflush(fp);
  124. }
  125. if (comparator != NULL) {
  126. if (Comparator_Act(comparator, buf, loadSize) == FALSE) {
  127. success = FALSE;
  128. }
  129. }
  130. osal_free(buf);
  131. ret = VPU_EncUpdateBitstreamBuffer(*handle, loadSize);
  132. if( ret != RETCODE_SUCCESS ) {
  133. VLOG(ERR, "VPU_EncUpdateBitstreamBuffer failed Error code is 0x%x \n", ret );
  134. success = FALSE;
  135. }
  136. }
  137. return success;
  138. }
  139. BOOL BitstreamReader_Destroy(
  140. BitstreamReader reader
  141. )
  142. {
  143. AbstractBitstreamReader* absReader = (AbstractBitstreamReader*)reader;
  144. if (absReader == NULL) {
  145. VLOG(ERR, "%s:%d Invalid handle\n", __FUNCTION__, __LINE__);
  146. return FALSE;
  147. }
  148. osal_fclose(absReader->fp);
  149. osal_free(absReader);
  150. return TRUE;
  151. }