bsfeeder_size_plus_es_impl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // SPDX-License-Identifier: LGPL-2.1 OR BSD-3-Clause
  2. //--=========================================================================--
  3. // This file is a part of VPU Reference API project
  4. //-----------------------------------------------------------------------------
  5. //
  6. // This confidential and proprietary software may be used only
  7. // as authorized by a licensing agreement from Chips&Media Inc.
  8. // In the event of publication, the following notice is applicable:
  9. //
  10. // (C) COPYRIGHT CHIPS&MEDIA INC.
  11. // ALL RIGHTS RESERVED
  12. //
  13. // The entire notice above must be reproduced on all authorized
  14. // copies.
  15. //
  16. //--=========================================================================--
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <errno.h>
  21. #include "vpuapifunc.h"
  22. #include "main_helper.h"
  23. #define MAX_FEEDING_SIZE 0x400000 /* 4MBytes */
  24. typedef struct struct_ReaderConext {
  25. osal_file_t fp;
  26. Uint32 feedingSize;
  27. BOOL eos;
  28. } ReaderContext;
  29. void* BSFeederSizePlusEs_Create(
  30. const char* path
  31. )
  32. {
  33. osal_file_t fp = NULL;
  34. ReaderContext* context=NULL;
  35. if ((fp=osal_fopen(path, "rb")) == NULL) {
  36. VLOG(ERR, "%s:%d failed to open %s\n", __FUNCTION__, __LINE__, path);
  37. return NULL;
  38. }
  39. context = (ReaderContext*)osal_malloc(sizeof(ReaderContext));
  40. if (context == NULL) {
  41. VLOG(ERR, "%s:%d failed to allocate memory\n", __FUNCTION__, __LINE__);
  42. osal_fclose(fp);
  43. return NULL;
  44. }
  45. context->fp = fp;
  46. context->feedingSize = 0;
  47. context->eos = FALSE;
  48. return (void*)context;
  49. }
  50. BOOL BSFeederSizePlusEs_Destroy(
  51. void* feeder
  52. )
  53. {
  54. ReaderContext* context = (ReaderContext*)feeder;
  55. if (context == NULL) {
  56. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  57. return FALSE;
  58. }
  59. if (context->fp)
  60. osal_fclose(context->fp);
  61. osal_free(context);
  62. return TRUE;
  63. }
  64. Int32 BSFeederSizePlusEs_Act(
  65. void* feeder,
  66. BSChunk* chunk
  67. )
  68. {
  69. ReaderContext* context = (ReaderContext*)feeder;
  70. size_t nRead;
  71. Uint32 chunkSize = 0;
  72. if (context == NULL) {
  73. VLOG(ERR, "%s:%d Null handle\n", __FUNCTION__, __LINE__);
  74. return FALSE;
  75. }
  76. if (context->eos == TRUE) {
  77. return 0;
  78. }
  79. osal_fread(&chunkSize, 1, 4, context->fp);
  80. nRead = osal_fread(chunk->data, 1, chunkSize, context->fp);
  81. if (nRead < 0) {
  82. VLOG(ERR, "%s:%d failed to read bitstream(errno: %d)\n", __FUNCTION__, __LINE__, errno);
  83. return 0;
  84. }
  85. else if (nRead < chunkSize) {
  86. context->eos = TRUE;
  87. }
  88. chunk->size = chunkSize;
  89. return nRead;
  90. }
  91. BOOL BSFeederSizePlusEs_Rewind(
  92. void* feeder
  93. )
  94. {
  95. ReaderContext* context = (ReaderContext*)feeder;
  96. Int32 ret;
  97. if ((ret=osal_fseek(context->fp, 0, SEEK_SET)) != 0) {
  98. VLOG(ERR, "%s:%d failed osal_fseek(ret:%d)\n", __FUNCTION__, __LINE__, ret);
  99. return FALSE;
  100. }
  101. return TRUE;
  102. }