buffer_internal.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <stdint.h>
  21. #include "buffer.h"
  22. #include "thread.h"
  23. /**
  24. * The buffer is always treated as read-only.
  25. */
  26. #define BUFFER_FLAG_READONLY (1 << 0)
  27. /**
  28. * The buffer was av_realloc()ed, so it is reallocatable.
  29. */
  30. #define BUFFER_FLAG_REALLOCATABLE (1 << 1)
  31. struct AVBuffer {
  32. uint8_t *data; /**< data described by this buffer */
  33. int size; /**< size of data in bytes */
  34. /**
  35. * number of existing AVBufferRef instances referring to this buffer
  36. */
  37. volatile int refcount;
  38. /**
  39. * a callback for freeing the data
  40. */
  41. void (*free)(void *opaque, uint8_t *data);
  42. /**
  43. * an opaque pointer, to be used by the freeing callback
  44. */
  45. void *opaque;
  46. /**
  47. * A combination of BUFFER_FLAG_*
  48. */
  49. int flags;
  50. };
  51. typedef struct BufferPoolEntry {
  52. uint8_t *data;
  53. /*
  54. * Backups of the original opaque/free of the AVBuffer corresponding to
  55. * data. They will be used to free the buffer when the pool is freed.
  56. */
  57. void *opaque;
  58. void (*free)(void *opaque, uint8_t *data);
  59. AVBufferPool *pool;
  60. struct BufferPoolEntry * volatile next;
  61. } BufferPoolEntry;
  62. struct AVBufferPool {
  63. AVMutex mutex;
  64. BufferPoolEntry * volatile pool;
  65. /*
  66. * This is used to track when the pool is to be freed.
  67. * The pointer to the pool itself held by the caller is considered to
  68. * be one reference. Each buffer requested by the caller increases refcount
  69. * by one, returning the buffer to the pool decreases it by one.
  70. * refcount reaches zero when the buffer has been uninited AND all the
  71. * buffers have been released, then it's safe to free the pool and all
  72. * the buffers in it.
  73. */
  74. volatile int refcount;
  75. volatile int nb_allocated;
  76. int size;
  77. AVBufferRef* (*alloc)(int size);
  78. };
  79. #endif /* AVUTIL_BUFFER_INTERNAL_H */