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