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