buffer_internal.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_BUFFER_INTERNAL_H
  19. #define AVUTIL_BUFFER_INTERNAL_H
  20. #include <stdatomic.h>
  21. #include <stdint.h>
  22. #include "buffer.h"
  23. #include "thread.h"
  24. /**
  25. * The buffer is always treated as read-only.
  26. */
  27. #define BUFFER_FLAG_READONLY (1 << 0)
  28. /**
  29. * The buffer was av_realloc()ed, so it is reallocatable.
  30. */
  31. #define BUFFER_FLAG_REALLOCATABLE (1 << 1)
  32. struct AVBuffer {
  33. uint8_t *data; /**< data described by this buffer */
  34. int size; /**< size of data in bytes */
  35. /**
  36. * number of existing AVBufferRef instances referring to this buffer
  37. */
  38. atomic_uint refcount;
  39. /**
  40. * a callback for freeing the data
  41. */
  42. void (*free)(void *opaque, uint8_t *data);
  43. /**
  44. * an opaque pointer, to be used by the freeing callback
  45. */
  46. void *opaque;
  47. /**
  48. * A combination of BUFFER_FLAG_*
  49. */
  50. int flags;
  51. };
  52. typedef struct BufferPoolEntry {
  53. uint8_t *data;
  54. /*
  55. * Backups of the original opaque/free of the AVBuffer corresponding to
  56. * data. They will be used to free the buffer when the pool is freed.
  57. */
  58. void *opaque;
  59. void (*free)(void *opaque, uint8_t *data);
  60. AVBufferPool *pool;
  61. struct BufferPoolEntry *next;
  62. } BufferPoolEntry;
  63. struct AVBufferPool {
  64. AVMutex mutex;
  65. BufferPoolEntry *pool;
  66. /*
  67. * This is used to track when the pool is to be freed.
  68. * The pointer to the pool itself held by the caller is considered to
  69. * be one reference. Each buffer requested by the caller increases refcount
  70. * by one, returning the buffer to the pool decreases it by one.
  71. * refcount reaches zero when the buffer has been uninited AND all the
  72. * buffers have been released, then it's safe to free the pool and all
  73. * the buffers in it.
  74. */
  75. atomic_uint refcount;
  76. int size;
  77. void *opaque;
  78. AVBufferRef* (*alloc)(int size);
  79. AVBufferRef* (*alloc2)(void *opaque, int size);
  80. void (*pool_free)(void *opaque);
  81. };
  82. #endif /* AVUTIL_BUFFER_INTERNAL_H */