samplefmt.h 10 KB

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