audio_data.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVRESAMPLE_AUDIO_DATA_H
  21. #define AVRESAMPLE_AUDIO_DATA_H
  22. #include <stdint.h>
  23. #include "libavutil/audio_fifo.h"
  24. #include "libavutil/log.h"
  25. #include "libavutil/samplefmt.h"
  26. #include "avresample.h"
  27. #include "internal.h"
  28. /**
  29. * Audio buffer used for intermediate storage between conversion phases.
  30. */
  31. struct AudioData {
  32. const AVClass *class; /**< AVClass for logging */
  33. uint8_t *data[AVRESAMPLE_MAX_CHANNELS]; /**< data plane pointers */
  34. uint8_t *buffer; /**< data buffer */
  35. unsigned int buffer_size; /**< allocated buffer size */
  36. int allocated_samples; /**< number of samples the buffer can hold */
  37. int nb_samples; /**< current number of samples */
  38. enum AVSampleFormat sample_fmt; /**< sample format */
  39. int channels; /**< channel count */
  40. int allocated_channels; /**< allocated channel count */
  41. int is_planar; /**< sample format is planar */
  42. int planes; /**< number of data planes */
  43. int sample_size; /**< bytes per sample */
  44. int stride; /**< sample byte offset within a plane */
  45. int read_only; /**< data is read-only */
  46. int allow_realloc; /**< realloc is allowed */
  47. int ptr_align; /**< minimum data pointer alignment */
  48. int samples_align; /**< allocated samples alignment */
  49. const char *name; /**< name for debug logging */
  50. };
  51. int ff_audio_data_set_channels(AudioData *a, int channels);
  52. /**
  53. * Initialize AudioData using a given source.
  54. *
  55. * This does not allocate an internal buffer. It only sets the data pointers
  56. * and audio parameters.
  57. *
  58. * @param a AudioData struct
  59. * @param src source data pointers
  60. * @param plane_size plane size, in bytes.
  61. * This can be 0 if unknown, but that will lead to
  62. * optimized functions not being used in many cases,
  63. * which could slow down some conversions.
  64. * @param channels channel count
  65. * @param nb_samples number of samples in the source data
  66. * @param sample_fmt sample format
  67. * @param read_only indicates if buffer is read only or read/write
  68. * @param name name for debug logging (can be NULL)
  69. * @return 0 on success, negative AVERROR value on error
  70. */
  71. int ff_audio_data_init(AudioData *a, uint8_t **src, int plane_size, int channels,
  72. int nb_samples, enum AVSampleFormat sample_fmt,
  73. int read_only, const char *name);
  74. /**
  75. * Allocate AudioData.
  76. *
  77. * This allocates an internal buffer and sets audio parameters.
  78. *
  79. * @param channels channel count
  80. * @param nb_samples number of samples to allocate space for
  81. * @param sample_fmt sample format
  82. * @param name name for debug logging (can be NULL)
  83. * @return newly allocated AudioData struct, or NULL on error
  84. */
  85. AudioData *ff_audio_data_alloc(int channels, int nb_samples,
  86. enum AVSampleFormat sample_fmt,
  87. const char *name);
  88. /**
  89. * Reallocate AudioData.
  90. *
  91. * The AudioData must have been previously allocated with ff_audio_data_alloc().
  92. *
  93. * @param a AudioData struct
  94. * @param nb_samples number of samples to allocate space for
  95. * @return 0 on success, negative AVERROR value on error
  96. */
  97. int ff_audio_data_realloc(AudioData *a, int nb_samples);
  98. /**
  99. * Free AudioData.
  100. *
  101. * The AudioData must have been previously allocated with ff_audio_data_alloc().
  102. *
  103. * @param a AudioData struct
  104. */
  105. void ff_audio_data_free(AudioData **a);
  106. /**
  107. * Copy data from one AudioData to another.
  108. *
  109. * @param out output AudioData
  110. * @param in input AudioData
  111. * @param map channel map, NULL if not remapping
  112. * @return 0 on success, negative AVERROR value on error
  113. */
  114. int ff_audio_data_copy(AudioData *out, AudioData *in, ChannelMapInfo *map);
  115. /**
  116. * Append data from one AudioData to the end of another.
  117. *
  118. * @param dst destination AudioData
  119. * @param dst_offset offset, in samples, to start writing, relative to the
  120. * start of dst
  121. * @param src source AudioData
  122. * @param src_offset offset, in samples, to start copying, relative to the
  123. * start of the src
  124. * @param nb_samples number of samples to copy
  125. * @return 0 on success, negative AVERROR value on error
  126. */
  127. int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
  128. int src_offset, int nb_samples);
  129. /**
  130. * Drain samples from the start of the AudioData.
  131. *
  132. * Remaining samples are shifted to the start of the AudioData.
  133. *
  134. * @param a AudioData struct
  135. * @param nb_samples number of samples to drain
  136. */
  137. void ff_audio_data_drain(AudioData *a, int nb_samples);
  138. /**
  139. * Add samples in AudioData to an AVAudioFifo.
  140. *
  141. * @param af Audio FIFO Buffer
  142. * @param a AudioData struct
  143. * @param offset number of samples to skip from the start of the data
  144. * @param nb_samples number of samples to add to the FIFO
  145. * @return number of samples actually added to the FIFO, or
  146. * negative AVERROR code on error
  147. */
  148. int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset,
  149. int nb_samples);
  150. /**
  151. * Read samples from an AVAudioFifo to AudioData.
  152. *
  153. * @param af Audio FIFO Buffer
  154. * @param a AudioData struct
  155. * @param nb_samples number of samples to read from the FIFO
  156. * @return number of samples actually read from the FIFO, or
  157. * negative AVERROR code on error
  158. */
  159. int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples);
  160. #endif /* AVRESAMPLE_AUDIO_DATA_H */