This is a maintenance fork
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 lines
6.4 KiB

2 years ago
  1. /*-*- mode:c;indent-tabs-mode:t;c-basic-offset:4;tab-width:4;coding:utf-8 -*-│
  2. vi: set et ft=c ts=4 sw=4 fenc=utf-8 :vi
  3. PL_MPEG - MPEG1 Video decoder, MP2 Audio decoder, MPEG-PS demuxer
  4. Dominic Szablewski - https://phoboslab.org │
  5. The MIT License(MIT)
  6. Copyright(c) 2019 Dominic Szablewski
  7. Permission is hereby granted, free of charge, to any person obtaining
  8. a copy of this software and associated documentation files(the
  9. "Software"), to deal in the Software without restriction, including
  10. without limitation the rights to use, copy, modify, merge, publish,
  11. distribute, sublicense, and / or sell copies of the Software, and to
  12. permit persons to whom the Software is furnished to do so, subject to
  13. the following conditions:
  14. The above copyright notice and this permission notice shall be
  15. included in all copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25. #include "dsp/mpeg/buffer.h"
  26. #include "dsp/mpeg/mpeg.h"
  27. #include "libc/calls/calls.h"
  28. #include "libc/log/check.h"
  29. #include "libc/mem/mem.h"
  30. #include "libc/stdio/stdio.h"
  31. #include "libc/str/str.h"
  32. #include "libc/sysv/consts/madv.h"
  33. asm(".ident\t\"\\n\\n\
  34. PL_MPEG (MIT License)\\n\
  35. Copyright(c) 2019 Dominic Szablewski\\n\
  36. https://phoboslab.org\"");
  37. asm(".include \"libc/disclaimer.inc\"");
  38. /* clang-format off */
  39. // -----------------------------------------------------------------------------
  40. // plm_buffer implementation
  41. plm_buffer_t *plm_buffer_create_with_filename(const char *filename) {
  42. FILE *fh = fopen(filename, "rb");
  43. if (!fh) {
  44. return NULL;
  45. }
  46. fadvise(fileno(fh), 0, 0, MADV_SEQUENTIAL);
  47. return plm_buffer_create_with_file(fh, true);
  48. }
  49. plm_buffer_t *plm_buffer_create_with_file(FILE *fh, int close_when_done) {
  50. plm_buffer_t *b;
  51. b = plm_buffer_create_with_capacity(PLM_BUFFER_DEFAULT_SIZE);
  52. b->fh = fh;
  53. b->close_when_done = close_when_done;
  54. b->mode = PLM_BUFFER_MODE_FILE;
  55. plm_buffer_set_load_callback(b, plm_buffer_load_file_callback, NULL);
  56. return b;
  57. }
  58. plm_buffer_t *plm_buffer_create_with_memory(unsigned char *bytes, size_t length, int free_when_done) {
  59. plm_buffer_t *b;
  60. b = memalign(alignof(plm_buffer_t), sizeof(plm_buffer_t));
  61. memset(b, 0, sizeof(plm_buffer_t));
  62. b->capacity = length;
  63. b->length = length;
  64. b->free_when_done = free_when_done;
  65. b->bytes = bytes;
  66. b->mode = PLM_BUFFER_MODE_FIXED_MEM;
  67. return b;
  68. }
  69. plm_buffer_t * plm_buffer_create_with_capacity(size_t capacity) {
  70. plm_buffer_t *b;
  71. b = memalign(alignof(plm_buffer_t), sizeof(plm_buffer_t));
  72. memset(b, 0, sizeof(plm_buffer_t));
  73. b->capacity = capacity;
  74. b->free_when_done = true;
  75. b->bytes = (unsigned char *)malloc(capacity);
  76. b->mode = PLM_BUFFER_MODE_DYNAMIC_MEM;
  77. return b;
  78. }
  79. void plm_buffer_destroy(plm_buffer_t *self) {
  80. if (self->fh && self->close_when_done) {
  81. fclose(self->fh);
  82. }
  83. if (self->free_when_done) {
  84. free(self->bytes);
  85. }
  86. free(self);
  87. }
  88. size_t plm_buffer_write(plm_buffer_t *self, unsigned char *bytes, size_t length) {
  89. if (self->mode == PLM_BUFFER_MODE_FIXED_MEM) {
  90. return 0;
  91. }
  92. // This should be a ring buffer, but instead it just shifts all unread data
  93. // to the beginning of the buffer and appends new data at the end. Seems
  94. // to be good enough.
  95. plm_buffer_discard_read_bytes(self);
  96. // Do we have to resize to fit the new data?
  97. size_t bytes_available = self->capacity - self->length;
  98. if (bytes_available < length) {
  99. size_t new_size = self->capacity;
  100. do {
  101. new_size *= 2;
  102. } while (new_size - self->length < length);
  103. self->bytes = (unsigned char *)realloc(self->bytes, new_size);
  104. self->capacity = new_size;
  105. }
  106. memcpy(self->bytes + self->length, bytes, length);
  107. self->length += length;
  108. return length;
  109. }
  110. void plm_buffer_set_load_callback(plm_buffer_t *self, plm_buffer_load_callback fp, void *user) {
  111. self->load_callback = fp;
  112. self->load_callback_user_data = user;
  113. }
  114. void plm_buffer_rewind(plm_buffer_t *self) {
  115. if (self->fh) {
  116. fseek(self->fh, 0, SEEK_SET);
  117. self->length = 0;
  118. }
  119. if (self->mode != PLM_BUFFER_MODE_FIXED_MEM) {
  120. self->length = 0;
  121. }
  122. self->bit_index = 0;
  123. }
  124. void plm_buffer_discard_read_bytes(plm_buffer_t *self) {
  125. size_t byte_pos = self->bit_index >> 3;
  126. if (byte_pos == self->length) {
  127. self->bit_index = 0;
  128. self->length = 0;
  129. }
  130. else if (byte_pos > 0) {
  131. memmove(self->bytes, self->bytes + byte_pos, self->length - byte_pos);
  132. self->bit_index -= byte_pos << 3;
  133. self->length -= byte_pos;
  134. }
  135. }
  136. void plm_buffer_load_file_callback(plm_buffer_t *self, void *user) {
  137. plm_buffer_discard_read_bytes(self);
  138. unsigned bytes_available = self->capacity - self->length;
  139. unsigned bytes_read = fread(self->bytes + self->length, 1, bytes_available, self->fh);
  140. self->length += bytes_read;
  141. }