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