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 module ffmpeg.libavutil.samplefmt;
20 
21 extern (C):
22 import ffmpeg; @nogc nothrow:
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  */
55 enum AVSampleFormat
56 {
57     AV_SAMPLE_FMT_NONE = -1,
58     AV_SAMPLE_FMT_U8 = 0, ///< unsigned 8 bits
59     AV_SAMPLE_FMT_S16 = 1, ///< signed 16 bits
60     AV_SAMPLE_FMT_S32 = 2, ///< signed 32 bits
61     AV_SAMPLE_FMT_FLT = 3, ///< float
62     AV_SAMPLE_FMT_DBL = 4, ///< double
63 
64     AV_SAMPLE_FMT_U8P = 5, ///< unsigned 8 bits, planar
65     AV_SAMPLE_FMT_S16P = 6, ///< signed 16 bits, planar
66     AV_SAMPLE_FMT_S32P = 7, ///< signed 32 bits, planar
67     AV_SAMPLE_FMT_FLTP = 8, ///< float, planar
68     AV_SAMPLE_FMT_DBLP = 9, ///< double, planar
69     AV_SAMPLE_FMT_S64 = 10, ///< signed 64 bits
70     AV_SAMPLE_FMT_S64P = 11, ///< signed 64 bits, planar
71 
72     AV_SAMPLE_FMT_NB = 12 ///< Number of sample formats. DO NOT USE if linking dynamically
73 }
74 
75 /**
76  * Return the name of sample_fmt, or NULL if sample_fmt is not
77  * recognized.
78  */
79 const(char)* av_get_sample_fmt_name (AVSampleFormat sample_fmt);
80 
81 /**
82  * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE
83  * on error.
84  */
85 AVSampleFormat av_get_sample_fmt (const(char)* name);
86 
87 /**
88  * Return the planar<->packed alternative form of the given sample format, or
89  * AV_SAMPLE_FMT_NONE on error. If the passed sample_fmt is already in the
90  * requested planar/packed format, the format returned is the same as the
91  * input.
92  */
93 AVSampleFormat av_get_alt_sample_fmt (AVSampleFormat sample_fmt, int planar);
94 
95 /**
96  * Get the packed alternative form of the given sample format.
97  *
98  * If the passed sample_fmt is already in packed format, the format returned is
99  * the same as the input.
100  *
101  * @return  the packed alternative form of the given sample format or
102             AV_SAMPLE_FMT_NONE on error.
103  */
104 AVSampleFormat av_get_packed_sample_fmt (AVSampleFormat sample_fmt);
105 
106 /**
107  * Get the planar alternative form of the given sample format.
108  *
109  * If the passed sample_fmt is already in planar format, the format returned is
110  * the same as the input.
111  *
112  * @return  the planar alternative form of the given sample format or
113             AV_SAMPLE_FMT_NONE on error.
114  */
115 AVSampleFormat av_get_planar_sample_fmt (AVSampleFormat sample_fmt);
116 
117 /**
118  * Generate a string corresponding to the sample format with
119  * sample_fmt, or a header if sample_fmt is negative.
120  *
121  * @param buf the buffer where to write the string
122  * @param buf_size the size of buf
123  * @param sample_fmt the number of the sample format to print the
124  * corresponding info string, or a negative value to print the
125  * corresponding header.
126  * @return the pointer to the filled buffer or NULL if sample_fmt is
127  * unknown or in case of other errors
128  */
129 char* av_get_sample_fmt_string (char* buf, int buf_size, AVSampleFormat sample_fmt);
130 
131 /**
132  * Return number of bytes per sample.
133  *
134  * @param sample_fmt the sample format
135  * @return number of bytes per sample or zero if unknown for the given
136  * sample format
137  */
138 int av_get_bytes_per_sample (AVSampleFormat sample_fmt);
139 
140 /**
141  * Check if the sample format is planar.
142  *
143  * @param sample_fmt the sample format to inspect
144  * @return 1 if the sample format is planar, 0 if it is interleaved
145  */
146 int av_sample_fmt_is_planar (AVSampleFormat sample_fmt);
147 
148 /**
149  * Get the required buffer size for the given audio parameters.
150  *
151  * @param[out] linesize calculated linesize, may be NULL
152  * @param nb_channels   the number of channels
153  * @param nb_samples    the number of samples in a single channel
154  * @param sample_fmt    the sample format
155  * @param align         buffer size alignment (0 = default, 1 = no alignment)
156  * @return              required buffer size, or negative error code on failure
157  */
158 int av_samples_get_buffer_size (
159     int* linesize,
160     int nb_channels,
161     int nb_samples,
162     AVSampleFormat sample_fmt,
163     int align_);
164 
165 /**
166  * @}
167  *
168  * @defgroup lavu_sampmanip Samples manipulation
169  *
170  * Functions that manipulate audio samples
171  * @{
172  */
173 
174 /**
175  * Fill plane data pointers and linesize for samples with sample
176  * format sample_fmt.
177  *
178  * The audio_data array is filled with the pointers to the samples data planes:
179  * for planar, set the start point of each channel's data within the buffer,
180  * for packed, set the start point of the entire buffer only.
181  *
182  * The value pointed to by linesize is set to the aligned size of each
183  * channel's data buffer for planar layout, or to the aligned size of the
184  * buffer for all channels for packed layout.
185  *
186  * The buffer in buf must be big enough to contain all the samples
187  * (use av_samples_get_buffer_size() to compute its minimum size),
188  * otherwise the audio_data pointers will point to invalid data.
189  *
190  * @see enum AVSampleFormat
191  * The documentation for AVSampleFormat describes the data layout.
192  *
193  * @param[out] audio_data  array to be filled with the pointer for each channel
194  * @param[out] linesize    calculated linesize, may be NULL
195  * @param buf              the pointer to a buffer containing the samples
196  * @param nb_channels      the number of channels
197  * @param nb_samples       the number of samples in a single channel
198  * @param sample_fmt       the sample format
199  * @param align            buffer size alignment (0 = default, 1 = no alignment)
200  * @return                 >=0 on success or a negative error code on failure
201  * @todo return minimum size in bytes required for the buffer in case
202  * of success at the next bump
203  */
204 int av_samples_fill_arrays (
205     ubyte** audio_data,
206     int* linesize,
207     const(ubyte)* buf,
208     int nb_channels,
209     int nb_samples,
210     AVSampleFormat sample_fmt,
211     int align_);
212 
213 /**
214  * Allocate a samples buffer for nb_samples samples, and fill data pointers and
215  * linesize accordingly.
216  * The allocated samples buffer can be freed by using av_freep(&audio_data[0])
217  * Allocated data will be initialized to silence.
218  *
219  * @see enum AVSampleFormat
220  * The documentation for AVSampleFormat describes the data layout.
221  *
222  * @param[out] audio_data  array to be filled with the pointer for each channel
223  * @param[out] linesize    aligned size for audio buffer(s), may be NULL
224  * @param nb_channels      number of audio channels
225  * @param nb_samples       number of samples per channel
226  * @param align            buffer size alignment (0 = default, 1 = no alignment)
227  * @return                 >=0 on success or a negative error code on failure
228  * @todo return the size of the allocated buffer in case of success at the next bump
229  * @see av_samples_fill_arrays()
230  * @see av_samples_alloc_array_and_samples()
231  */
232 int av_samples_alloc (
233     ubyte** audio_data,
234     int* linesize,
235     int nb_channels,
236     int nb_samples,
237     AVSampleFormat sample_fmt,
238     int align_);
239 
240 /**
241  * Allocate a data pointers array, samples buffer for nb_samples
242  * samples, and fill data pointers and linesize accordingly.
243  *
244  * This is the same as av_samples_alloc(), but also allocates the data
245  * pointers array.
246  *
247  * @see av_samples_alloc()
248  */
249 int av_samples_alloc_array_and_samples (
250     ubyte*** audio_data,
251     int* linesize,
252     int nb_channels,
253     int nb_samples,
254     AVSampleFormat sample_fmt,
255     int align_);
256 
257 /**
258  * Copy samples from src to dst.
259  *
260  * @param dst destination array of pointers to data planes
261  * @param src source array of pointers to data planes
262  * @param dst_offset offset in samples at which the data will be written to dst
263  * @param src_offset offset in samples at which the data will be read from src
264  * @param nb_samples number of samples to be copied
265  * @param nb_channels number of audio channels
266  * @param sample_fmt audio sample format
267  */
268 int av_samples_copy (
269     ubyte** dst,
270     ubyte** src,
271     int dst_offset,
272     int src_offset,
273     int nb_samples,
274     int nb_channels,
275     AVSampleFormat sample_fmt);
276 
277 /**
278  * Fill an audio buffer with silence.
279  *
280  * @param audio_data  array of pointers to data planes
281  * @param offset      offset in samples at which to start filling
282  * @param nb_samples  number of samples to fill
283  * @param nb_channels number of audio channels
284  * @param sample_fmt  audio sample format
285  */
286 int av_samples_set_silence (
287     ubyte** audio_data,
288     int offset,
289     int nb_samples,
290     int nb_channels,
291     AVSampleFormat sample_fmt);
292 
293 /**
294  * @}
295  * @}
296  */
297 /* AVUTIL_SAMPLEFMT_H */