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