libavutil: add utility functions to simplify allocation of audio buffers.
[libav.git] / libavutil / samplefmt.h
CommitLineData
262d1c5d 1/*
2912e87a 2 * This file is part of Libav.
262d1c5d 3 *
2912e87a 4 * Libav is free software; you can redistribute it and/or
262d1c5d
SS
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 *
2912e87a 9 * Libav is distributed in the hope that it will be useful,
262d1c5d
SS
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
2912e87a 15 * License along with Libav; if not, write to the Free Software
262d1c5d
SS
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
737eb597
RT
19#ifndef AVUTIL_SAMPLEFMT_H
20#define AVUTIL_SAMPLEFMT_H
6cce172f 21
a6703faa
SS
22#include "avutil.h"
23
262d1c5d
SS
24/**
25 * all in native-endian format
26 */
27enum AVSampleFormat {
28 AV_SAMPLE_FMT_NONE = -1,
29 AV_SAMPLE_FMT_U8, ///< unsigned 8 bits
30 AV_SAMPLE_FMT_S16, ///< signed 16 bits
31 AV_SAMPLE_FMT_S32, ///< signed 32 bits
32 AV_SAMPLE_FMT_FLT, ///< float
33 AV_SAMPLE_FMT_DBL, ///< double
8889cc4f
JR
34
35 AV_SAMPLE_FMT_U8P, ///< unsigned 8 bits, planar
36 AV_SAMPLE_FMT_S16P, ///< signed 16 bits, planar
37 AV_SAMPLE_FMT_S32P, ///< signed 32 bits, planar
38 AV_SAMPLE_FMT_FLTP, ///< float, planar
39 AV_SAMPLE_FMT_DBLP, ///< double, planar
40
737eb597 41 AV_SAMPLE_FMT_NB ///< Number of sample formats. DO NOT USE if linking dynamically
262d1c5d
SS
42};
43
d63e456a
SS
44/**
45 * Return the name of sample_fmt, or NULL if sample_fmt is not
46 * recognized.
47 */
48const char *av_get_sample_fmt_name(enum AVSampleFormat sample_fmt);
49
50/**
51 * Return a sample format corresponding to name, or AV_SAMPLE_FMT_NONE
52 * on error.
53 */
54enum AVSampleFormat av_get_sample_fmt(const char *name);
55
56/**
57 * Generate a string corresponding to the sample format with
58 * sample_fmt, or a header if sample_fmt is negative.
59 *
60 * @param buf the buffer where to write the string
61 * @param buf_size the size of buf
62 * @param sample_fmt the number of the sample format to print the
63 * corresponding info string, or a negative value to print the
64 * corresponding header.
65 * @return the pointer to the filled buffer or NULL if sample_fmt is
66 * unknown or in case of other errors
67 */
68char *av_get_sample_fmt_string(char *buf, int buf_size, enum AVSampleFormat sample_fmt);
69
a6703faa
SS
70#if FF_API_GET_BITS_PER_SAMPLE_FMT
71/**
72 * @deprecated Use av_get_bytes_per_sample() instead.
73 */
74attribute_deprecated
75int av_get_bits_per_sample_fmt(enum AVSampleFormat sample_fmt);
76#endif
77
6f84cd12 78/**
a6703faa 79 * Return number of bytes per sample.
6f84cd12
SS
80 *
81 * @param sample_fmt the sample format
a6703faa 82 * @return number of bytes per sample or zero if unknown for the given
6f84cd12
SS
83 * sample format
84 */
a6703faa 85int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt);
6f84cd12 86
8889cc4f
JR
87/**
88 * Check if the sample format is planar.
89 *
90 * @param sample_fmt the sample format to inspect
91 * @return 1 if the sample format is planar, 0 if it is interleaved
92 */
93int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt);
94
bbb46f3e
JR
95/**
96 * Get the required buffer size for the given audio parameters.
97 *
98 * @param[out] linesize calculated linesize, may be NULL
99 * @param nb_channels the number of channels
100 * @param nb_samples the number of samples in a single channel
101 * @param sample_fmt the sample format
102 * @return required buffer size, or negative error code on failure
103 */
104int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples,
105 enum AVSampleFormat sample_fmt, int align);
106
107/**
108 * Fill channel data pointers and linesize for samples with sample
109 * format sample_fmt.
110 *
111 * The pointers array is filled with the pointers to the samples data:
112 * for planar, set the start point of each channel's data within the buffer,
113 * for packed, set the start point of the entire buffer only.
114 *
115 * The linesize array is filled with the aligned size of each channel's data
116 * buffer for planar layout, or the aligned size of the buffer for all channels
117 * for packed layout.
118 *
119 * @param[out] audio_data array to be filled with the pointer for each channel
120 * @param[out] linesize calculated linesize
121 * @param buf the pointer to a buffer containing the samples
122 * @param nb_channels the number of channels
123 * @param nb_samples the number of samples in a single channel
124 * @param sample_fmt the sample format
125 * @param align buffer size alignment (1 = no alignment required)
126 * @return 0 on success or a negative error code on failure
127 */
128int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, uint8_t *buf,
129 int nb_channels, int nb_samples,
130 enum AVSampleFormat sample_fmt, int align);
131
132/**
133 * Allocate a samples buffer for nb_samples samples, and fill data pointers and
134 * linesize accordingly.
135 * The allocated samples buffer can be freed by using av_freep(&audio_data[0])
136 *
137 * @param[out] audio_data array to be filled with the pointer for each channel
138 * @param[out] linesize aligned size for audio buffer(s)
139 * @param nb_channels number of audio channels
140 * @param nb_samples number of samples per channel
141 * @param align buffer size alignment (1 = no alignment required)
142 * @return 0 on success or a negative error code on failure
143 * @see av_samples_fill_arrays()
144 */
145int av_samples_alloc(uint8_t **audio_data, int *linesize, int nb_channels,
146 int nb_samples, enum AVSampleFormat sample_fmt, int align);
147
737eb597 148#endif /* AVUTIL_SAMPLEFMT_H */