bsfeeder_fixedsize_impl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 <stdio.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <errno.h>
  29. #include "vpuapifunc.h"
  30. #include "main_helper.h"
  31. #define MAX_FEEDING_SIZE 0x400000 /* 4MBytes */
  32. #define DEFAULT_FEEDING_SIZE 0x20000 /* 128KBytes */
  33. typedef struct FeederFixedContext {
  34. osal_file_t* fp;
  35. Uint32 feedingSize;
  36. BOOL eos;
  37. } FeederFixedContext;
  38. void* BSFeederFixedSize_Create(
  39. const char* path,
  40. CodStd codecId
  41. )
  42. {
  43. osal_file_t* fp = NULL;
  44. FeederFixedContext* context=NULL;
  45. UNREFERENCED_PARAMETER(codecId);
  46. if ((fp=osal_fopen(path, "rb")) == NULL) {
  47. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, path);
  48. return NULL;
  49. }
  50. context = (FeederFixedContext*)osal_malloc(sizeof(FeederFixedContext));
  51. if (context == NULL) {
  52. VLOG(ERR, "%s:%d failed to allocate memory\n", __FUNCTION__, __LINE__);
  53. osal_fclose(fp);
  54. return NULL;
  55. }
  56. context->fp = fp;
  57. context->feedingSize = DEFAULT_FEEDING_SIZE;
  58. context->eos = FALSE;
  59. return (void*)context;
  60. }
  61. BOOL BSFeederFixedSize_Destroy(
  62. void* feeder
  63. )
  64. {
  65. FeederFixedContext* context = (FeederFixedContext*)feeder;
  66. if (context == NULL) {
  67. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  68. return FALSE;
  69. }
  70. if (context->fp)
  71. osal_fclose(context->fp);
  72. osal_free(context);
  73. return TRUE;
  74. }
  75. Int32 BSFeederFixedSize_Act(
  76. void* feeder,
  77. BSChunk* chunk
  78. )
  79. {
  80. FeederFixedContext* context = (FeederFixedContext*)feeder;
  81. size_t nRead;
  82. Uint32 size;
  83. Uint32 feedingSize;
  84. if (context == NULL) {
  85. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  86. return 0;
  87. }
  88. if (context->eos == TRUE) {
  89. chunk->eos = TRUE;
  90. return 0;
  91. }
  92. feedingSize = context->feedingSize;
  93. if (feedingSize == 0) {
  94. Uint32 KB = 1024;
  95. BOOL probability10;
  96. srand((Uint32)time(NULL));
  97. feedingSize = rand() % MAX_FEEDING_SIZE;
  98. probability10 = (BOOL)((feedingSize%100) < 10);
  99. if (feedingSize < KB) {
  100. if (probability10 == FALSE)
  101. feedingSize *= 100;
  102. }
  103. }
  104. size = (chunk->size < feedingSize) ? chunk->size : feedingSize;
  105. do {
  106. nRead = osal_fread(chunk->data, 1, size, context->fp);
  107. if ((Int32)nRead < 0) {
  108. VLOG(ERR, "%s:%d failed to read bitstream(errno: %d)\n", __FUNCTION__, __LINE__, errno);
  109. return 0;
  110. }
  111. else if (nRead < size) {
  112. context->eos = TRUE;
  113. chunk->eos = TRUE;
  114. }
  115. } while (FALSE);
  116. return nRead;
  117. }
  118. BOOL BSFeederFixedSize_Rewind(
  119. void* feeder
  120. )
  121. {
  122. FeederFixedContext* context = (FeederFixedContext*)feeder;
  123. Int32 ret;
  124. if ((ret=osal_fseek(context->fp, 0, SEEK_SET)) != 0) {
  125. VLOG(ERR, "%s:%d Failed to fseek(ret: %d)\n", __FUNCTION__, __LINE__, ret);
  126. return FALSE;
  127. }
  128. context->eos = FALSE;
  129. return TRUE;
  130. }
  131. void BSFeederFixedSize_SetFeedingSize(
  132. void* feeder,
  133. Uint32 feedingSize
  134. )
  135. {
  136. FeederFixedContext* context = (FeederFixedContext*)feeder;
  137. if (feedingSize > 0) {
  138. context->feedingSize = feedingSize;
  139. }
  140. }