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.

245 lines
8.8KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef AVUTIL_SAMPLEFMT_H
  19. #define AVUTIL_SAMPLEFMT_H
  20. #include <stdint.h>
  21. #include "avutil.h"
  22. #include "attributes.h"
  23. /**
  24. * @addtogroup lavu_audio
  25. * @{
  26. *
  27. * @defgroup lavu_sampfmts Audio sample formats
  28. *
  29. * Audio sample format enumeration and related convenience functions.
  30. * @{
  31. */
  32. /**
  33. * Audio Sample Formats
  34. *
  35. * @par
  36. * The data described by the sample format is always in native-endian order.
  37. * Sample values can be expressed by native C types, hence the lack of a signed
  38. * 24-bit sample format even though it is a common raw audio data format.
  39. *
  40. * @par
  41. * The floating-point formats are based on full volume being in the range
  42. * [-1.0, 1.0]. Any values outside this range are beyond full volume level.
  43. *
  44. * @par
  45. * The data layout as used in av_samples_fill_arrays() and elsewhere in Libav
  46. * (such as AVFrame in libavcodec) is as follows:
  47. *
  48. * @par
  49. * For planar sample formats, each audio channel is in a separate data plane,
  50. * and linesize is the buffer size, in bytes, for a single plane. All data
  51. * planes must be the same size. For packed sample formats, only the first data
  52. * plane is used, and samples for each channel are interleaved. In this case,
  53. * linesize is the buffer size, in bytes, for the 1 plane.
  54. */
  55. enum AVSampleFormat {
  56. AV_SAMPLE_FMT_NONE = -1,
  57. AV_SAMPLE_FMT_U8, ///< unsigned 8 bits
  58. AV_SAMPLE_FMT_S16, ///< signed 16 bits
  59. AV_SAMPLE_FMT_S32, ///< signed 32 bits
  60. AV_SAMPLE_FMT_FLT, ///< float
  61. AV_SAMPLE_FMT_DBL, ///< double
  62. AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar
  63. AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar
  64. AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
  65. AV_SAMPLE_FMT_FLTP, ///< float, planar
  66. AV_SAMPLE_FMT_DBLP, ///< double, planar
  67. AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically
  68. };
  69. /**
  70. * Return the name of sample_fmt, or NULL if sample_fmt is not
  71. * recognized.
  72. */
  73. const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt);
  74. /**
  75. * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE
  76. * on error.
  77. */
  78. enum AVSampleFormat av_get_sample_fmt(const char *name);
  79. /**
  80. * Get the packed alternative form of the given sample format.
  81. *
  82. * If the passed sample_fmt is already in packed format, the format returned is
  83. * the same as the input.
  84. *
  85. * @return the packed alternative form of the given sample format or
  86. AV_SAMPLE_FMT_NONE on error.
  87. */
  88. enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt);
  89. /**
  90. * Get the planar alternative form of the given sample format.
  91. *
  92. * If the passed sample_fmt is already in planar format, the format returned is
  93. * the same as the input.
  94. *
  95. * @return the planar alternative form of the given sample format or
  96. AV_SAMPLE_FMT_NONE on error.
  97. */
  98. enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt);
  99. /**
  100. * Generate a string corresponding to the sample format with
  101. * sample_fmt, or a header if sample_fmt is negative.
  102. *
  103. * @param buf the buffer where to write the string
  104. * @param buf_size the size of buf
  105. * @param sample_fmt the number of the sample format to print the
  106. * corresponding info string, or a negative value to print the
  107. * corresponding header.
  108. * @return the pointer to the filled buffer or NULL if sample_fmt is
  109. * unknown or in case of other errors
  110. */
  111. char *av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt);
  112. /**
  113. * Return number of bytes per sample.
  114. *
  115. * @param sample_fmt the sample format
  116. * @return number of bytes per sample or zero if unknown for the given
  117. * sample format
  118. */
  119. int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt);
  120. /**
  121. * Check if the sample format is planar.
  122. *
  123. * @param sample_fmt the sample format to inspect
  124. * @return 1 if the sample format is planar, 0 if it is interleaved
  125. */
  126. int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt);
  127. /**
  128. * Get the required buffer size for the given audio parameters.
  129. *
  130. * @param[out] linesize calculated linesize, may be NULL
  131. * @param nb_channels the number of channels
  132. * @param nb_samples the number of samples in a single channel
  133. * @param sample_fmt the sample format
  134. * @param align buffer size alignment (0 = default, 1 = no alignment)
  135. * @return required buffer size, or negative error code on failure
  136. */
  137. int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
  138. enum AVSampleFormat sample_fmt, int align);
  139. /**
  140. * @}
  141. *
  142. * @defgroup lavu_sampmanip Samples manipulation
  143. *
  144. * Functions that manipulate audio samples
  145. * @{
  146. */
  147. /**
  148. * Fill channel data pointers and linesize for samples with sample
  149. * format sample_fmt.
  150. *
  151. * The pointers array is filled with the pointers to the samples data:
  152. * for planar, set the start point of each channel's data within the buffer,
  153. * for packed, set the start point of the entire buffer only.
  154. *
  155. * The linesize array is filled with the aligned size of each channel's data
  156. * buffer for planar layout, or the aligned size of the buffer for all channels
  157. * for packed layout.
  158. *
  159. * @see enum AVSampleFormat
  160. * The documentation for AVSampleFormat describes the data layout.
  161. *
  162. * @param[out] audio_data array to be filled with the pointer for each channel
  163. * @param[out] linesize calculated linesize, may be NULL
  164. * @param buf the pointer to a buffer containing the samples
  165. * @param nb_channels the number of channels
  166. * @param nb_samples the number of samples in a single channel
  167. * @param sample_fmt the sample format
  168. * @param align buffer size alignment (0 = default, 1 = no alignment)
  169. * @return 0 on success or a negative error code on failure
  170. */
  171. int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
  172. const uint8_t *buf,
  173. int nb_channels, int nb_samples,
  174. enum AVSampleFormat sample_fmt, int align);
  175. /**
  176. * Allocate a samples buffer for nb_samples samples, and fill data pointers and
  177. * linesize accordingly.
  178. * The allocated samples buffer can be freed by using av_freep(&audio_data[0])
  179. * Allocated data will be initialized to silence.
  180. *
  181. * @see enum AVSampleFormat
  182. * The documentation for AVSampleFormat describes the data layout.
  183. *
  184. * @param[out] audio_data array to be filled with the pointer for each channel
  185. * @param[out] linesize aligned size for audio buffer(s), may be NULL
  186. * @param nb_channels number of audio channels
  187. * @param nb_samples number of samples per channel
  188. * @param align buffer size alignment (0 = default, 1 = no alignment)
  189. * @return 0 on success or a negative error code on failure
  190. * @see av_samples_fill_arrays()
  191. */
  192. int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
  193. int nb_samples, enum AVSampleFormat sample_fmt, int align);
  194. /**
  195. * Copy samples from src to dst.
  196. *
  197. * @param dst destination array of pointers to data planes
  198. * @param src source array of pointers to data planes
  199. * @param dst_offset offset in samples at which the data will be written to dst
  200. * @param src_offset offset in samples at which the data will be read from src
  201. * @param nb_samples number of samples to be copied
  202. * @param nb_channels number of audio channels
  203. * @param sample_fmt audio sample format
  204. */
  205. int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
  206. int src_offset, int nb_samples, int nb_channels,
  207. enum AVSampleFormat sample_fmt);
  208. /**
  209. * Fill an audio buffer with silence.
  210. *
  211. * @param audio_data array of pointers to data planes
  212. * @param offset offset in samples at which to start filling
  213. * @param nb_samples number of samples to fill
  214. * @param nb_channels number of audio channels
  215. * @param sample_fmt audio sample format
  216. */
  217. int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
  218. int nb_channels, enum AVSampleFormat sample_fmt);
  219. /**
  220. * @}
  221. * @}
  222. */
  223. #endif /* AVUTIL_SAMPLEFMT_H */