audio_fifo.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Audio FIFO
  3. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Audio FIFO Buffer
  24. */
  25. #ifndef AVUTIL_AUDIO_FIFO_H
  26. #define AVUTIL_AUDIO_FIFO_H
  27. #include "attributes.h"
  28. #include "samplefmt.h"
  29. /**
  30. * @addtogroup lavu_audio
  31. * @{
  32. *
  33. * @defgroup lavu_audiofifo Audio FIFO Buffer
  34. * @{
  35. */
  36. /**
  37. * Context for an Audio FIFO Buffer.
  38. *
  39. * - Operates at the sample level rather than the byte level.
  40. * - Supports multiple channels with either planar or packed sample format.
  41. * - Automatic reallocation when writing to a full buffer.
  42. */
  43. typedef struct AVAudioFifo AVAudioFifo;
  44. /**
  45. * Free an AVAudioFifo.
  46. *
  47. * @param af AVAudioFifo to free
  48. */
  49. void av_audio_fifo_free(AVAudioFifo *af);
  50. /**
  51. * Allocate an AVAudioFifo.
  52. *
  53. * @param sample_fmt sample format
  54. * @param channels number of channels
  55. * @param nb_samples initial allocation size, in samples
  56. * @return newly allocated AVAudioFifo, or NULL on error
  57. */
  58. AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
  59. int nb_samples);
  60. /**
  61. * Reallocate an AVAudioFifo.
  62. *
  63. * @param af AVAudioFifo to reallocate
  64. * @param nb_samples new allocation size, in samples
  65. * @return 0 if OK, or negative AVERROR code on failure
  66. */
  67. av_warn_unused_result
  68. int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples);
  69. /**
  70. * Write data to an AVAudioFifo.
  71. *
  72. * The AVAudioFifo will be reallocated automatically if the available space
  73. * is less than nb_samples.
  74. *
  75. * @see enum AVSampleFormat
  76. * The documentation for AVSampleFormat describes the data layout.
  77. *
  78. * @param af AVAudioFifo to write to
  79. * @param data audio data plane pointers
  80. * @param nb_samples number of samples to write
  81. * @return number of samples actually written, or negative AVERROR
  82. * code on failure. If successful, the number of samples
  83. * actually written will always be nb_samples.
  84. */
  85. int av_audio_fifo_write(AVAudioFifo *af, void * const *data, int nb_samples);
  86. /**
  87. * Peek data from an AVAudioFifo.
  88. *
  89. * @see enum AVSampleFormat
  90. * The documentation for AVSampleFormat describes the data layout.
  91. *
  92. * @param af AVAudioFifo to read from
  93. * @param data audio data plane pointers
  94. * @param nb_samples number of samples to peek
  95. * @return number of samples actually peek, or negative AVERROR code
  96. * on failure. The number of samples actually peek will not
  97. * be greater than nb_samples, and will only be less than
  98. * nb_samples if av_audio_fifo_size is less than nb_samples.
  99. */
  100. int av_audio_fifo_peek(const AVAudioFifo *af, void * const *data, int nb_samples);
  101. /**
  102. * Peek data from an AVAudioFifo.
  103. *
  104. * @see enum AVSampleFormat
  105. * The documentation for AVSampleFormat describes the data layout.
  106. *
  107. * @param af AVAudioFifo to read from
  108. * @param data audio data plane pointers
  109. * @param nb_samples number of samples to peek
  110. * @param offset offset from current read position
  111. * @return number of samples actually peek, or negative AVERROR code
  112. * on failure. The number of samples actually peek will not
  113. * be greater than nb_samples, and will only be less than
  114. * nb_samples if av_audio_fifo_size is less than nb_samples.
  115. */
  116. int av_audio_fifo_peek_at(const AVAudioFifo *af, void * const *data,
  117. int nb_samples, int offset);
  118. /**
  119. * Read data from an AVAudioFifo.
  120. *
  121. * @see enum AVSampleFormat
  122. * The documentation for AVSampleFormat describes the data layout.
  123. *
  124. * @param af AVAudioFifo to read from
  125. * @param data audio data plane pointers
  126. * @param nb_samples number of samples to read
  127. * @return number of samples actually read, or negative AVERROR code
  128. * on failure. The number of samples actually read will not
  129. * be greater than nb_samples, and will only be less than
  130. * nb_samples if av_audio_fifo_size is less than nb_samples.
  131. */
  132. int av_audio_fifo_read(AVAudioFifo *af, void * const *data, int nb_samples);
  133. /**
  134. * Drain data from an AVAudioFifo.
  135. *
  136. * Removes the data without reading it.
  137. *
  138. * @param af AVAudioFifo to drain
  139. * @param nb_samples number of samples to drain
  140. * @return 0 if OK, or negative AVERROR code on failure
  141. */
  142. int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples);
  143. /**
  144. * Reset the AVAudioFifo buffer.
  145. *
  146. * This empties all data in the buffer.
  147. *
  148. * @param af AVAudioFifo to reset
  149. */
  150. void av_audio_fifo_reset(AVAudioFifo *af);
  151. /**
  152. * Get the current number of samples in the AVAudioFifo available for reading.
  153. *
  154. * @param af the AVAudioFifo to query
  155. * @return number of samples available for reading
  156. */
  157. int av_audio_fifo_size(AVAudioFifo *af);
  158. /**
  159. * Get the current number of samples in the AVAudioFifo available for writing.
  160. *
  161. * @param af the AVAudioFifo to query
  162. * @return number of samples available for writing
  163. */
  164. int av_audio_fifo_space(AVAudioFifo *af);
  165. /**
  166. * @}
  167. * @}
  168. */
  169. #endif /* AVUTIL_AUDIO_FIFO_H */