2 * This file is part of Libav.
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.
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.
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
19 #ifndef AVUTIL_SAMPLEFMT_H
20 #define AVUTIL_SAMPLEFMT_H
25 #include "attributes.h"
28 * @addtogroup lavu_audio
31 * @defgroup lavu_sampfmts Audio sample formats
33 * Audio sample format enumeration and related convenience functions.
39 * Audio Sample Formats
42 * The data described by the sample format is always in native-endian order.
43 * Sample values can be expressed by native C types, hence the lack of a signed
44 * 24-bit sample format even though it is a common raw audio data format.
47 * The floating-point formats are based on full volume being in the range
48 * [-1.0, 1.0]. Any values outside this range are beyond full volume level.
51 * The data layout as used in av_samples_fill_arrays() and elsewhere in Libav
52 * (such as AVFrame in libavcodec) is as follows:
55 * For planar sample formats, each audio channel is in a separate data plane,
56 * and linesize is the buffer size, in bytes, for a single plane. All data
57 * planes must be the same size. For packed sample formats, only the first data
58 * plane is used, and samples for each channel are interleaved. In this case,
59 * linesize is the buffer size, in bytes, for the 1 plane.
62 AV_SAMPLE_FMT_NONE = -1,
63 AV_SAMPLE_FMT_U8, ///< unsigned 8 bits
64 AV_SAMPLE_FMT_S16, ///< signed 16 bits
65 AV_SAMPLE_FMT_S32, ///< signed 32 bits
66 AV_SAMPLE_FMT_FLT, ///< float
67 AV_SAMPLE_FMT_DBL, ///< double
69 AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar
70 AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar
71 AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
72 AV_SAMPLE_FMT_FLTP, ///< float, planar
73 AV_SAMPLE_FMT_DBLP, ///< double, planar
75 AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically
79 * Return the name of sample_fmt, or NULL if sample_fmt is not
82 const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt);
85 * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE
88 enum AVSampleFormat av_get_sample_fmt(const char *name);
91 * Get the packed alternative form of the given sample format.
93 * If the passed sample_fmt is already in packed format, the format returned is
94 * the same as the input.
96 * @return the packed alternative form of the given sample format or
97 AV_SAMPLE_FMT_NONE on error.
99 enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt);
102 * Get the planar alternative form of the given sample format.
104 * If the passed sample_fmt is already in planar format, the format returned is
105 * the same as the input.
107 * @return the planar alternative form of the given sample format or
108 AV_SAMPLE_FMT_NONE on error.
110 enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt);
113 * Generate a string corresponding to the sample format with
114 * sample_fmt, or a header if sample_fmt is negative.
116 * @param buf the buffer where to write the string
117 * @param buf_size the size of buf
118 * @param sample_fmt the number of the sample format to print the
119 * corresponding info string, or a negative value to print the
120 * corresponding header.
121 * @return the pointer to the filled buffer or NULL if sample_fmt is
122 * unknown or in case of other errors
124 char *av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt);
127 * Return number of bytes per sample.
129 * @param sample_fmt the sample format
130 * @return number of bytes per sample or zero if unknown for the given
133 int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt);
136 * Check if the sample format is planar.
138 * @param sample_fmt the sample format to inspect
139 * @return 1 if the sample format is planar, 0 if it is interleaved
141 int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt);
144 * Get the required buffer size for the given audio parameters.
146 * @param[out] linesize calculated linesize, may be NULL
147 * @param nb_channels the number of channels
148 * @param nb_samples the number of samples in a single channel
149 * @param sample_fmt the sample format
150 * @param align buffer size alignment (0 = default, 1 = no alignment)
151 * @return required buffer size, or negative error code on failure
153 int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
154 enum AVSampleFormat sample_fmt, int align);
159 * @defgroup lavu_sampmanip Samples manipulation
161 * Functions that manipulate audio samples
166 * Fill channel data pointers and linesize for samples with sample
169 * The pointers array is filled with the pointers to the samples data:
170 * for planar, set the start point of each channel's data within the buffer,
171 * for packed, set the start point of the entire buffer only.
173 * The linesize array is filled with the aligned size of each channel's data
174 * buffer for planar layout, or the aligned size of the buffer for all channels
177 * @see enum AVSampleFormat
178 * The documentation for AVSampleFormat describes the data layout.
180 * @param[out] audio_data array to be filled with the pointer for each channel
181 * @param[out] linesize calculated linesize, may be NULL
182 * @param buf the pointer to a buffer containing the samples
183 * @param nb_channels the number of channels
184 * @param nb_samples the number of samples in a single channel
185 * @param sample_fmt the sample format
186 * @param align buffer size alignment (0 = default, 1 = no alignment)
187 * @return 0 on success or a negative error code on failure
189 int av_samples_fill_arrays(uint8_t **audio_data, int *linesize,
191 int nb_channels, int nb_samples,
192 enum AVSampleFormat sample_fmt, int align);
195 * Allocate a samples buffer for nb_samples samples, and fill data pointers and
196 * linesize accordingly.
197 * The allocated samples buffer can be freed by using av_freep(&audio_data[0])
198 * Allocated data will be initialized to silence.
200 * @see enum AVSampleFormat
201 * The documentation for AVSampleFormat describes the data layout.
203 * @param[out] audio_data array to be filled with the pointer for each channel
204 * @param[out] linesize aligned size for audio buffer(s), may be NULL
205 * @param nb_channels number of audio channels
206 * @param nb_samples number of samples per channel
207 * @param align buffer size alignment (0 = default, 1 = no alignment)
208 * @return 0 on success or a negative error code on failure
209 * @see av_samples_fill_arrays()
211 int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
212 int nb_samples, enum AVSampleFormat sample_fmt, int align);
215 * Copy samples from src to dst.
217 * @param dst destination array of pointers to data planes
218 * @param src source array of pointers to data planes
219 * @param dst_offset offset in samples at which the data will be written to dst
220 * @param src_offset offset in samples at which the data will be read from src
221 * @param nb_samples number of samples to be copied
222 * @param nb_channels number of audio channels
223 * @param sample_fmt audio sample format
225 int av_samples_copy(uint8_t **dst, uint8_t * const *src, int dst_offset,
226 int src_offset, int nb_samples, int nb_channels,
227 enum AVSampleFormat sample_fmt);
230 * Fill an audio buffer with silence.
232 * @param audio_data array of pointers to data planes
233 * @param offset offset in samples at which to start filling
234 * @param nb_samples number of samples to fill
235 * @param nb_channels number of audio channels
236 * @param sample_fmt audio sample format
238 int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples,
239 int nb_channels, enum AVSampleFormat sample_fmt);
245 #endif /* AVUTIL_SAMPLEFMT_H */