bsfeeder_size_plus_es_impl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. typedef struct FeederEsContext {
  33. osal_file_t fp;
  34. Uint32 feedingSize;
  35. BOOL eos;
  36. } FeederEsContext;
  37. void* BSFeederSizePlusEs_Create(
  38. const char* path,
  39. CodStd codecId
  40. )
  41. {
  42. osal_file_t fp = NULL;
  43. FeederEsContext* context=NULL;
  44. UNREFERENCED_PARAMETER(codecId);
  45. if ((fp=osal_fopen(path, "rb")) == NULL) {
  46. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, path);
  47. return NULL;
  48. }
  49. context = (FeederEsContext*)osal_malloc(sizeof(FeederEsContext));
  50. if (context == NULL) {
  51. VLOG(ERR, "%s:%d failed to allocate memory\n", __FUNCTION__, __LINE__);
  52. osal_fclose(fp);
  53. return NULL;
  54. }
  55. context->fp = fp;
  56. context->feedingSize = 0;
  57. context->eos = FALSE;
  58. return (void*)context;
  59. }
  60. BOOL BSFeederSizePlusEs_Destroy(
  61. void* feeder
  62. )
  63. {
  64. FeederEsContext* context = (FeederEsContext*)feeder;
  65. if (context == NULL) {
  66. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  67. return FALSE;
  68. }
  69. if (context->fp)
  70. osal_fclose(context->fp);
  71. osal_free(context);
  72. return TRUE;
  73. }
  74. Int32 BSFeederSizePlusEs_Act(
  75. void* feeder,
  76. BSChunk* chunk
  77. )
  78. {
  79. FeederEsContext* context = (FeederEsContext*)feeder;
  80. size_t nRead;
  81. Uint32 chunkSize = 0;
  82. if (context == NULL) {
  83. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  84. return FALSE;
  85. }
  86. if (context->eos == TRUE) {
  87. return 0;
  88. }
  89. osal_fread(&chunkSize, 1, 4, context->fp);
  90. nRead = osal_fread(chunk->data, 1, chunkSize, context->fp);
  91. if ((Int32)nRead < 0) {
  92. VLOG(ERR, "%s:%d failed to read bitstream(errno: %d)\n", __FUNCTION__, __LINE__, errno);
  93. return 0;
  94. }
  95. else if (nRead < chunkSize) {
  96. context->eos = TRUE;
  97. }
  98. chunk->size = chunkSize;
  99. return nRead;
  100. }
  101. BOOL BSFeederSizePlusEs_Rewind(
  102. void* feeder
  103. )
  104. {
  105. FeederEsContext* context = (FeederEsContext*)feeder;
  106. Int32 ret;
  107. if ((ret=osal_fseek(context->fp, 0, SEEK_SET)) != 0) {
  108. VLOG(ERR, "%s:%d failed osal_fseek(ret:%d)\n", __FUNCTION__, __LINE__, ret);
  109. return FALSE;
  110. }
  111. return TRUE;
  112. }