Go to the documentation of this file.
   34     if(!
s || 
s->in_convert) 
 
   43                         int log_offset, 
void *log_ctx) {
 
   52     s->log_level_offset = log_offset;
 
   90     memset(
a, 0, 
sizeof(*
a));
 
   94     s->in_buffer_index= 0;
 
   95     s->in_buffer_count= 0;
 
   96     s->resample_in_constraint= 0;
 
   97     memset(
s->in.ch, 0, 
sizeof(
s->in.ch));
 
   98     memset(
s->out.ch, 0, 
sizeof(
s->out.ch));
 
  115     s->delayed_samples_fixup = 0;
 
  128             s->resampler->free(&
s->resample);
 
  140     char l1[1024], l2[1024];
 
  157     if(
s->out_sample_rate <= 0){
 
  185     s->int_sample_fmt= 
s->user_int_sample_fmt;
 
  187     s->dither.method = 
s->user_dither_method;
 
  202     if (
s->used_ch_layout.nb_channels != 
s->in_ch_layout.nb_channels)
 
  216                  s->rematrix_volume!=1.0 ||
 
  223            && 
s->out_sample_rate==
s->in_sample_rate) {
 
  231            && 
s->out_sample_rate==
s->in_sample_rate
 
  237                  && 
s->out_sample_rate == 
s->in_sample_rate
 
  262         if (!
s->async && 
s->min_compensation >= FLT_MAX/2)
 
  266             s->outpts   = 
s->firstpts_in_samples * 
s->out_sample_rate;
 
  271         if (
s->min_compensation >= FLT_MAX/2)
 
  272             s->min_compensation = 0.001;
 
  273         if (
s->async > 1.0001) {
 
  274             s->max_soft_compensation = 
s->async / (
double) 
s->in_sample_rate;
 
  279         s->resample = 
s->resampler->init(
s->resample, 
s->out_sample_rate, 
s->in_sample_rate, 
s->filter_size, 
s->phase_shift, 
s->linear_interp, 
s->cutoff, 
s->int_sample_fmt, 
s->filter_type, 
s->kaiser_beta, 
s->precision, 
s->cheby, 
s->exact_rational);
 
  285         s->resampler->free(&
s->resample);
 
  296 #define RSC 1 //FIXME finetune 
  302         s->out.ch_count = 
s->out_ch_layout.nb_channels;
 
  314         av_log(
s, 
AV_LOG_ERROR, 
"Input channel layout %s mismatches specified channel count %d\n", l1, 
s->used_ch_layout.nb_channels);
 
  322                "but there is not enough information to do it\n", l1, l2);
 
  333     s->drop_temp= 
s->out;
 
  338     if(!
s->resample && !
s->rematrix && !
s->channel_map && !
s->dither.method){
 
  347                                              s->int_sample_fmt, 
s->out.ch_count, 
NULL, 0);
 
  349     if (!
s->in_convert || !
s->out_convert) {
 
  360         s->midbuf.ch_count= 
s->used_ch_layout.nb_channels;
 
  362             s->in_buffer.ch_count= 
s->used_ch_layout.nb_channels;
 
  364     if(!
s->resample_first){
 
  365         s->midbuf.ch_count= 
s->out.ch_count;
 
  367             s->in_buffer.ch_count = 
s->out.ch_count;
 
  379     s->dither.noise = 
s->preout;
 
  380     s->dither.temp  = 
s->preout;
 
  382         s->dither.noise.bps = 4;
 
  384         s->dither.noise_scale = 1;
 
  387     if(
s->rematrix || 
s->dither.method) {
 
  404     if(count < 0 || count > INT_MAX/2/
a->bps/
a->ch_count)
 
  407     if(
a->count >= count)
 
  421     for(
i=0; 
i<
a->ch_count; 
i++){
 
  422         a->ch[
i]= 
a->data + 
i*(
a->planar ? countb : 
a->bps);
 
  423         if(
a->count && 
a->planar) memcpy(
a->ch[
i], old.
ch[
i], 
a->count*
a->bps);
 
  425     if(
a->count && !
a->planar) memcpy(
a->ch[0], old.
ch[0], 
a->count*
a->ch_count*
a->bps);
 
  439         for(ch=0; ch<
out->ch_count; ch++)
 
  440             memcpy(
out->ch[ch], 
in->
ch[ch], count*
out->bps);
 
  449         memset(
out->ch, 0, 
sizeof(
out->ch));
 
  450     }
else if(
out->planar){
 
  451         for(
i=0; 
i<
out->ch_count; 
i++)
 
  452             out->ch[
i]= in_arg[
i];
 
  454         for(
i=0; 
i<
out->ch_count; 
i++)
 
  462         for(
i=0; 
i<
out->ch_count; 
i++)
 
  463             in_arg[
i]= 
out->ch[
i];
 
  465         in_arg[0]= 
out->ch[0];
 
  476         for(ch=0; ch<
out->ch_count; ch++)
 
  479         for(ch=
out->ch_count-1; ch>=0; ch--)
 
  480             out->ch[ch]= 
in->
ch[0] + (ch + count*
out->ch_count) * 
out->bps;
 
  489                              const AudioData * in_param, 
int in_count){
 
  502     border = 
s->resampler->invert_initial_buffer(
s->resample, &
s->in_buffer,
 
  503                  &
in, in_count, &
s->in_buffer_index, &
s->in_buffer_count);
 
  504     if (border == INT_MAX) {
 
  506     } 
else if (border < 0) {
 
  511         s->resample_in_constraint = 0;
 
  516         if(!
s->resample_in_constraint && 
s->in_buffer_count){
 
  518             ret= 
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
tmp, 
s->in_buffer_count, &consumed);
 
  522             s->in_buffer_count -= consumed;
 
  523             s->in_buffer_index += consumed;
 
  527             if(
s->in_buffer_count <= border){
 
  529                 in_count += 
s->in_buffer_count;
 
  530                 s->in_buffer_count=0;
 
  531                 s->in_buffer_index=0;
 
  536         if((
s->flushed || in_count > padless) && !
s->in_buffer_count){
 
  537             s->in_buffer_index=0;
 
  538             ret= 
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
in, 
FFMAX(in_count-padless, 0), &consumed);
 
  542             in_count -= consumed;
 
  547         size= 
s->in_buffer_index + 
s->in_buffer_count + in_count;
 
  548         if(   
size > 
s->in_buffer.count
 
  551             copy(&
s->in_buffer, &
tmp, 
s->in_buffer_count);
 
  552             s->in_buffer_index=0;
 
  559             if(
s->in_buffer_count && 
s->in_buffer_count+2 < count && out_count) count= 
s->in_buffer_count+2;
 
  561             buf_set(&
tmp, &
s->in_buffer, 
s->in_buffer_index + 
s->in_buffer_count);
 
  563             s->in_buffer_count += count;
 
  567             s->resample_in_constraint= 0;
 
  568             if(
s->in_buffer_count != count || in_count)
 
  578     s->resample_in_constraint= !!out_count;
 
  600     if(
s->resample_first){
 
  601         av_assert0(
s->midbuf.ch_count == 
s->used_ch_layout.nb_channels);
 
  614     midbuf_tmp= 
s->midbuf;
 
  616     preout_tmp= 
s->preout;
 
  622     if(
s->resample_first ? !
s->resample : !
s->rematrix)
 
  625     if(
s->resample_first ? !
s->rematrix : !
s->resample)
 
  628     if(
s->int_sample_fmt == 
s->out_sample_fmt && 
s->out.planar
 
  631             out_count= 
FFMIN(out_count, in_count); 
 
  645     if(
s->resample_first){
 
  661         if(
s->dither.method){
 
  663             int dither_count= 
FFMAX(out_count, 1<<16);
 
  666                 conv_src = &
s->dither.temp;
 
  674                 for(ch=0; ch<
s->dither.noise.ch_count; ch++)
 
  675                     if((
ret=
swri_get_dither(
s, 
s->dither.noise.ch[ch], 
s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828
U, 
s->dither.noise.fmt))<0)
 
  679             if(
s->dither.noise_pos + out_count > 
s->dither.noise.count)
 
  680                 s->dither.noise_pos = 0;
 
  683                 if (
s->mix_2_1_simd) {
 
  684                     int len1= out_count&~15;
 
  689                             s->mix_2_1_simd(conv_src->
ch[ch], 
preout->
ch[ch], 
s->dither.noise.ch[ch] + 
s->dither.noise.bps * 
s->dither.noise_pos, &
s->native_simd_one, 0, 0, len1);
 
  690                     if(out_count != len1)
 
  692                             s->mix_2_1_f(conv_src->
ch[ch] + off, 
preout->
ch[ch] + off, 
s->dither.noise.ch[ch] + 
s->dither.noise.bps * 
s->dither.noise_pos + off, &
s->native_one, 0, 0, out_count - len1);
 
  695                         s->mix_2_1_f(conv_src->
ch[ch], 
preout->
ch[ch], 
s->dither.noise.ch[ch] + 
s->dither.noise.bps * 
s->dither.noise_pos, &
s->native_one, 0, 0, out_count);
 
  698                 switch(
s->int_sample_fmt) {
 
  705             s->dither.noise_pos += out_count;
 
  714     return !!
s->in_buffer.ch_count;
 
  718                                           uint8_t * 
const *out_arg, 
int out_count,
 
  719                                     const uint8_t * 
const *in_arg,  
int in_count)
 
  729 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1 
  733     while(
s->drop_output > 0){
 
  736 #define MAX_DROP_STEP 16384 
  741         s->drop_output *= -1; 
 
  743         s->drop_output *= -1;
 
  746             s->drop_output -= 
ret;
 
  747             if (!
s->drop_output && !out_arg)
 
  759                 s->resampler->flush(
s);
 
  760             s->resample_in_constraint = 0;
 
  762         }
else if(!
s->in_buffer_count){
 
  772         if(
ret>0 && !
s->drop_output)
 
  789             s->in_buffer_count -= 
ret;
 
  790             s->in_buffer_index += 
ret;
 
  793             if(!
s->in_buffer_count)
 
  794                 s->in_buffer_index = 0;
 
  798             size= 
s->in_buffer_index + 
s->in_buffer_count + in_count - out_count;
 
  800             if(in_count > out_count) { 
 
  801                 if(   
size > 
s->in_buffer.count
 
  804                     copy(&
s->in_buffer, &
tmp, 
s->in_buffer_count);
 
  805                     s->in_buffer_index=0;
 
  821                 buf_set(&
tmp, &
s->in_buffer, 
s->in_buffer_index + 
s->in_buffer_count);
 
  823                 s->in_buffer_count += in_count;
 
  826         if(ret2>0 && !
s->drop_output)
 
  827             s->outpts += ret2 * (
int64_t)
s->in_sample_rate;
 
  828         av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
 
  835     s->drop_output += count;
 
  837     if(
s->drop_output <= 0)
 
  851 #define MAX_SILENCE_STEP 16384 
  861     if(
s->silence.planar) 
for(
i=0; 
i<
s->silence.ch_count; 
i++) {
 
  862         memset(
s->silence.ch[
i], 
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps);
 
  864         memset(
s->silence.ch[0], 
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps*
s->silence.ch_count);
 
  873     if (
s->resampler && 
s->resample){
 
  874         return s->resampler->get_delay(
s, 
base);
 
  876         return (
s->in_buffer_count*
base + (
s->in_sample_rate>>1))/ 
s->in_sample_rate;
 
  887     if (
s->resampler && 
s->resample) {
 
  888         if (!
s->resampler->get_out_samples)
 
  890         out_samples = 
s->resampler->get_out_samples(
s, in_samples);
 
  892         out_samples = 
s->in_buffer_count + in_samples;
 
  896     if (out_samples > INT_MAX)
 
  905     if (!
s || compensation_distance < 0)
 
  907     if (!compensation_distance && sample_delta)
 
  915     if (!
s->resampler->set_compensation){
 
  918         return s->resampler->set_compensation(
s->resample, sample_delta, compensation_distance);
 
  927         s->outpts = 
s->firstpts = 
pts;
 
  929     if(
s->min_compensation >= FLT_MAX) {
 
  933         double fdelta = 
delta /(
double)(
s->in_sample_rate * (
int64_t)
s->out_sample_rate);
 
  935         if(
fabs(fdelta) > 
s->min_compensation) {
 
  936             if(
s->outpts == 
s->firstpts || 
fabs(fdelta) > 
s->min_hard_compensation){
 
  943             } 
else if(
s->soft_compensation_duration && 
s->max_soft_compensation) {
 
  944                 int duration = 
s->out_sample_rate * 
s->soft_compensation_duration;
 
  
@ AV_SAMPLE_FMT_FLTP
float, planar
#define AV_LOG_WARNING
Something somehow does not look correct.
static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count, AudioData *in, int in_count)
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
const struct Resampler swri_resampler
void swri_noise_shaping_int16(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
int in_sample_rate
input sample rate
static void free_temp(AudioData *a)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance)
Activate resampling compensation ("soft" compensation).
int out_sample_rate
output sample rate
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
AVChannelLayout out_ch_layout
output channel layout
AVChannelLayout user_in_chlayout
User set input channel layout.
#define AV_LOG_VERBOSE
Detailed information.
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map)
Set a customized input channel mapping.
enum AVChannelOrder order
Channel order used in this layout.
static void copy(AudioData *out, AudioData *in, int count)
int nb_channels
Number of channels in this layout.
int in_buffer_index
cached buffer position
void swri_noise_shaping_float(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
@ SWR_ENGINE_SOXR
SoX Resampler.
static void buf_set(AudioData *out, AudioData *in, int count)
out may be equal in.
enum AVSampleFormat out_sample_fmt
output sample format
int swri_realloc_audio(AudioData *a, int count)
int swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
static void fill_audiodata(AudioData *out, uint8_t *const in_arg[SWR_CH_MAX])
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
#define ss(width, name, subs,...)
int64_t swr_next_pts(struct SwrContext *s, int64_t pts)
Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate...
int attribute_align_arg swr_convert(struct SwrContext *s, uint8_t *const *out_arg, int out_count, const uint8_t *const *in_arg, int in_count)
Convert audio.
int64_t swr_get_delay(struct SwrContext *s, int64_t base)
Gets the delay the next input sample will experience relative to the next output sample.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
AudioData postin
post-input audio data: used for rematrix/resample
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int swr_inject_silence(struct SwrContext *s, int count)
Injects the specified number of silence samples.
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
AVChannelLayout user_out_chlayout
User set output channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy)
@ SWR_DITHER_NS
not part of API/ABI
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int planar
1 if planar audio, 0 otherwise
struct AudioConvert * in_convert
input conversion context
static const uint8_t channel_map[8][8]
int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len)
Convert between audio sample formats.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
The libswresample context.
uint8_t * data
samples buffer
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
AVChannelLayout in_ch_layout
input channel layout
void swri_noise_shaping_int32(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
AudioData midbuf
intermediate audio data (postin/preout)
static __device__ float fabs(float a)
static void reversefill_audiodata(AudioData *out, uint8_t *in_arg[SWR_CH_MAX])
void * log_ctx
parent logging context
static int resample(SwrContext *s, AudioData *out_param, int out_count, const AudioData *in_param, int in_count)
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int ch_count
number of channels
#define attribute_align_arg
int swr_alloc_set_opts2(struct SwrContext **ps, const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
@ AV_SAMPLE_FMT_NB
Number of sample formats. DO NOT USE if linking dynamically.
const struct Resampler swri_soxr_resampler
An AVChannelLayout holds information about the channel layout of audio data.
AudioData preout
pre-output audio data: used for rematrix/resample
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
#define SWR_FLAG_RESAMPLE
Force resampling even if equal sample rate.
int swr_drop_output(struct SwrContext *s, int count)
Drops the specified number of output samples.
#define AV_NOPTS_VALUE
Undefined timestamp value.
av_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
av_cold void swri_rematrix_free(SwrContext *s)
@ SWR_ENGINE_SWR
SW Resampler.
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
AudioConvert * swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags)
Create an audio sample format converter context.
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define i(width, name, range_min, range_max)
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
int swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt)
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
AVSampleFormat
Audio sample formats.
float max_soft_compensation
swr maximum soft compensation in seconds over soft_compensation_duration
av_cold int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
void swri_noise_shaping_double(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
static void clear_context(SwrContext *s)
void * av_calloc(size_t nmemb, size_t size)
enum AVSampleFormat in_sample_fmt
input sample format
static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt)
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
enum AVSampleFormat fmt
sample format
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
void swri_audio_convert_free(AudioConvert **ctx)
Free audio sample format converter context.
@ AV_SAMPLE_FMT_DBLP
double, planar
av_cold int swri_rematrix_init(SwrContext *s)
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
int swr_get_out_samples(struct SwrContext *s, int in_samples)
Find an upper bound on the number of samples that the next swr_convert call will output,...
AudioData in
input audio data
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.