FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
audio_data.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #ifndef AVRESAMPLE_AUDIO_DATA_H
22 #define AVRESAMPLE_AUDIO_DATA_H
23 
24 #include <stdint.h>
25 
26 #include "libavutil/audio_fifo.h"
27 #include "libavutil/log.h"
28 #include "libavutil/samplefmt.h"
29 #include "avresample.h"
30 #include "internal.h"
31 
32 int ff_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels);
33 
34 /**
35  * Audio buffer used for intermediate storage between conversion phases.
36  */
37 struct AudioData {
38  const AVClass *class; /**< AVClass for logging */
39  uint8_t *data[AVRESAMPLE_MAX_CHANNELS]; /**< data plane pointers */
40  uint8_t *buffer; /**< data buffer */
41  unsigned int buffer_size; /**< allocated buffer size */
42  int allocated_samples; /**< number of samples the buffer can hold */
43  int nb_samples; /**< current number of samples */
44  enum AVSampleFormat sample_fmt; /**< sample format */
45  int channels; /**< channel count */
46  int allocated_channels; /**< allocated channel count */
47  int is_planar; /**< sample format is planar */
48  int planes; /**< number of data planes */
49  int sample_size; /**< bytes per sample */
50  int stride; /**< sample byte offset within a plane */
51  int read_only; /**< data is read-only */
52  int allow_realloc; /**< realloc is allowed */
53  int ptr_align; /**< minimum data pointer alignment */
54  int samples_align; /**< allocated samples alignment */
55  const char *name; /**< name for debug logging */
56 };
57 
58 int ff_audio_data_set_channels(AudioData *a, int channels);
59 
60 /**
61  * Initialize AudioData using a given source.
62  *
63  * This does not allocate an internal buffer. It only sets the data pointers
64  * and audio parameters.
65  *
66  * @param a AudioData struct
67  * @param src source data pointers
68  * @param plane_size plane size, in bytes.
69  * This can be 0 if unknown, but that will lead to
70  * optimized functions not being used in many cases,
71  * which could slow down some conversions.
72  * @param channels channel count
73  * @param nb_samples number of samples in the source data
74  * @param sample_fmt sample format
75  * @param read_only indicates if buffer is read only or read/write
76  * @param name name for debug logging (can be NULL)
77  * @return 0 on success, negative AVERROR value on error
78  */
79 int ff_audio_data_init(AudioData *a, uint8_t **src, int plane_size, int channels,
80  int nb_samples, enum AVSampleFormat sample_fmt,
81  int read_only, const char *name);
82 
83 /**
84  * Allocate AudioData.
85  *
86  * This allocates an internal buffer and sets audio parameters.
87  *
88  * @param channels channel count
89  * @param nb_samples number of samples to allocate space for
90  * @param sample_fmt sample format
91  * @param name name for debug logging (can be NULL)
92  * @return newly allocated AudioData struct, or NULL on error
93  */
94 AudioData *ff_audio_data_alloc(int channels, int nb_samples,
95  enum AVSampleFormat sample_fmt,
96  const char *name);
97 
98 /**
99  * Reallocate AudioData.
100  *
101  * The AudioData must have been previously allocated with ff_audio_data_alloc().
102  *
103  * @param a AudioData struct
104  * @param nb_samples number of samples to allocate space for
105  * @return 0 on success, negative AVERROR value on error
106  */
107 int ff_audio_data_realloc(AudioData *a, int nb_samples);
108 
109 /**
110  * Free AudioData.
111  *
112  * The AudioData must have been previously allocated with ff_audio_data_alloc().
113  *
114  * @param a AudioData struct
115  */
117 
118 /**
119  * Copy data from one AudioData to another.
120  *
121  * @param out output AudioData
122  * @param in input AudioData
123  * @param map channel map, NULL if not remapping
124  * @return 0 on success, negative AVERROR value on error
125  */
127 
128 /**
129  * Append data from one AudioData to the end of another.
130  *
131  * @param dst destination AudioData
132  * @param dst_offset offset, in samples, to start writing, relative to the
133  * start of dst
134  * @param src source AudioData
135  * @param src_offset offset, in samples, to start copying, relative to the
136  * start of the src
137  * @param nb_samples number of samples to copy
138  * @return 0 on success, negative AVERROR value on error
139  */
140 int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src,
141  int src_offset, int nb_samples);
142 
143 /**
144  * Drain samples from the start of the AudioData.
145  *
146  * Remaining samples are shifted to the start of the AudioData.
147  *
148  * @param a AudioData struct
149  * @param nb_samples number of samples to drain
150  */
151 void ff_audio_data_drain(AudioData *a, int nb_samples);
152 
153 /**
154  * Add samples in AudioData to an AVAudioFifo.
155  *
156  * @param af Audio FIFO Buffer
157  * @param a AudioData struct
158  * @param offset number of samples to skip from the start of the data
159  * @param nb_samples number of samples to add to the FIFO
160  * @return number of samples actually added to the FIFO, or
161  * negative AVERROR code on error
162  */
164  int nb_samples);
165 
166 /**
167  * Read samples from an AVAudioFifo to AudioData.
168  *
169  * @param af Audio FIFO Buffer
170  * @param a AudioData struct
171  * @param nb_samples number of samples to read from the FIFO
172  * @return number of samples actually read from the FIFO, or
173  * negative AVERROR code on error
174  */
175 int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples);
176 
177 #endif /* AVRESAMPLE_AUDIO_DATA_H */
unsigned int buffer_size
allocated buffer size
Definition: audio_data.h:41
const char * name
name for debug logging
Definition: audio_data.h:55
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
void ff_audio_data_free(AudioData **a)
Free AudioData.
Definition: audio_data.c:216
Audio buffer used for intermediate storage between conversion phases.
Definition: audio_data.h:37
int allow_realloc
realloc is allowed
Definition: audio_data.h:52
int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src, int src_offset, int nb_samples)
Append data from one AudioData to the end of another.
Definition: audio_data.c:277
int ff_audio_data_copy(AudioData *out, AudioData *in, ChannelMapInfo *map)
Copy data from one AudioData to another.
Definition: audio_data.c:224
int nb_samples
current number of samples
Definition: audio_data.h:43
int ff_audio_data_set_channels(AudioData *a, int channels)
Definition: audio_data.c:59
int sample_size
bytes per sample
Definition: audio_data.h:49
int allocated_channels
allocated channel count
Definition: audio_data.h:46
uint8_t
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
Definition: audio_data.c:161
int read_only
data is read-only
Definition: audio_data.h:51
void ff_audio_data_drain(AudioData *a, int nb_samples)
Drain samples from the start of the AudioData.
Definition: audio_data.c:333
int ff_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels)
Definition: audio_data.c:51
int stride
sample byte offset within a plane
Definition: audio_data.h:50
int channels
channel count
Definition: audio_data.h:45
static const uint8_t offset[127][2]
Definition: vf_spp.c:92
int is_planar
sample format is planar
Definition: audio_data.h:47
uint8_t * buffer
data buffer
Definition: audio_data.h:40
Context for an Audio FIFO Buffer.
Definition: audio_fifo.c:34
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
Definition: audio_data.c:118
external API header
int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
Read samples from an AVAudioFifo to AudioData.
Definition: audio_data.c:365
AVS_Value src
Definition: avisynth_c.h:482
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:59
#define AVRESAMPLE_MAX_CHANNELS
Definition: avresample.h:104
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset, int nb_samples)
Add samples in AudioData to an AVAudioFifo.
Definition: audio_data.c:350
Describe the class of an AVClass context structure.
Definition: log.h:67
int samples_align
allocated samples alignment
Definition: audio_data.h:54
Audio FIFO Buffer.
int allocated_samples
number of samples the buffer can hold
Definition: audio_data.h:42
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> out
enum AVSampleFormat sample_fmt
sample format
Definition: audio_data.h:44
int ptr_align
minimum data pointer alignment
Definition: audio_data.h:53
int planes
number of data planes
Definition: audio_data.h:48
int ff_audio_data_init(AudioData *a, uint8_t **src, int plane_size, int channels, int nb_samples, enum AVSampleFormat sample_fmt, int read_only, const char *name)
Initialize AudioData using a given source.
Definition: audio_data.c:73
const char * name
Definition: opengl_enc.c:103