48 if(!
s ||
s->in_convert)
57 int log_offset,
void *log_ctx) {
66 s->log_level_offset = log_offset;
102 if (
a->ch_count == 1)
108 memset(
a, 0,
sizeof(*
a));
112 s->in_buffer_index= 0;
113 s->in_buffer_count= 0;
114 s->resample_in_constraint= 0;
115 memset(
s->in.ch, 0,
sizeof(
s->in.ch));
116 memset(
s->out.ch, 0,
sizeof(
s->out.ch));
133 s->delayed_samples_fixup = 0;
146 s->resampler->free(&
s->resample);
158 char l1[1024], l2[1024];
175 if(
s->out_sample_rate <= 0){
193 s->int_sample_fmt=
s->user_int_sample_fmt;
195 s->dither.method =
s->user_dither_method;
210 if (
s->used_ch_layout.nb_channels !=
s->in_ch_layout.nb_channels)
224 s->rematrix_volume!=1.0 ||
231 &&
s->out_sample_rate==
s->in_sample_rate) {
239 &&
s->out_sample_rate==
s->in_sample_rate
245 &&
s->out_sample_rate ==
s->in_sample_rate
270 if (!
s->async &&
s->min_compensation >= FLT_MAX/2)
274 s->outpts =
s->firstpts_in_samples *
s->out_sample_rate;
279 if (
s->min_compensation >= FLT_MAX/2)
280 s->min_compensation = 0.001;
281 if (
s->async > 1.0001) {
282 s->max_soft_compensation =
s->async / (
double)
s->in_sample_rate;
287 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);
293 s->resampler->free(&
s->resample);
310 s->out.ch_count =
s->out_ch_layout.nb_channels;
322 av_log(
s,
AV_LOG_ERROR,
"Input channel layout %s mismatches specified channel count %d\n", l1,
s->used_ch_layout.nb_channels);
330 "but there is not enough information to do it\n", l1, l2);
341 s->drop_temp=
s->out;
346 if(!
s->resample && !
s->rematrix && !
s->channel_map && !
s->dither.method){
355 s->int_sample_fmt,
s->out.ch_count,
NULL, 0);
357 if (!
s->in_convert || !
s->out_convert) {
368 s->midbuf.ch_count=
s->used_ch_layout.nb_channels;
370 s->in_buffer.ch_count=
s->used_ch_layout.nb_channels;
372 if(!
s->resample_first){
373 s->midbuf.ch_count=
s->out.ch_count;
375 s->in_buffer.ch_count =
s->out.ch_count;
387 s->dither.noise =
s->preout;
388 s->dither.temp =
s->preout;
390 s->dither.noise.bps = 4;
392 s->dither.noise_scale = 1;
395 if(
s->rematrix ||
s->dither.method) {
412 if(count < 0 || count > INT_MAX/2/
a->bps/
a->ch_count)
415 if(
a->count >= count)
429 for(
i=0;
i<
a->ch_count;
i++){
430 a->ch[
i]=
a->data +
i*(
a->planar ? countb :
a->bps);
431 if(
a->count &&
a->planar) memcpy(
a->ch[
i], old.
ch[
i],
a->count*
a->bps);
433 if(
a->count && !
a->planar) memcpy(
a->ch[0], old.
ch[0],
a->count*
a->ch_count*
a->bps);
447 for(ch=0; ch<
out->ch_count; ch++)
448 memcpy(
out->ch[ch],
in->
ch[ch], count*
out->bps);
457 memset(
out->ch, 0,
sizeof(
out->ch));
458 }
else if(
out->planar){
459 for(
i=0;
i<
out->ch_count;
i++)
460 out->ch[
i]= in_arg[
i];
462 for(
i=0;
i<
out->ch_count;
i++)
470 for(
i=0;
i<
out->ch_count;
i++)
471 in_arg[
i]=
out->ch[
i];
473 in_arg[0]=
out->ch[0];
484 for(ch=0; ch<
out->ch_count; ch++)
487 for(ch=
out->ch_count-1; ch>=0; ch--)
488 out->ch[ch]=
in->
ch[0] + (ch + count*
out->ch_count) *
out->bps;
497 const AudioData * in_param,
int in_count){
510 border =
s->resampler->invert_initial_buffer(
s->resample, &
s->in_buffer,
511 &
in, in_count, &
s->in_buffer_index, &
s->in_buffer_count);
512 if (border == INT_MAX) {
514 }
else if (border < 0) {
519 s->resample_in_constraint = 0;
523 int ret,
size, consumed;
524 if(!
s->resample_in_constraint &&
s->in_buffer_count){
526 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
tmp,
s->in_buffer_count, &consumed);
530 s->in_buffer_count -= consumed;
531 s->in_buffer_index += consumed;
535 if(
s->in_buffer_count <= border){
537 in_count +=
s->in_buffer_count;
538 s->in_buffer_count=0;
539 s->in_buffer_index=0;
544 if((
s->flushed || in_count > padless) && !
s->in_buffer_count){
545 s->in_buffer_index=0;
546 ret=
s->resampler->multiple_resample(
s->resample, &
out, out_count, &
in,
FFMAX(in_count-padless, 0), &consumed);
550 in_count -= consumed;
555 size=
s->in_buffer_index +
s->in_buffer_count + in_count;
556 if(
size >
s->in_buffer.count
559 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
560 s->in_buffer_index=0;
567 if(
s->in_buffer_count &&
s->in_buffer_count+2 < count && out_count) count=
s->in_buffer_count+2;
569 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
571 s->in_buffer_count += count;
575 s->resample_in_constraint= 0;
576 if(
s->in_buffer_count != count || in_count)
586 s->resample_in_constraint= !!out_count;
608 if(
s->resample_first){
609 av_assert0(
s->midbuf.ch_count ==
s->used_ch_layout.nb_channels);
622 midbuf_tmp=
s->midbuf;
624 preout_tmp=
s->preout;
630 if(
s->resample_first ? !
s->resample : !
s->rematrix)
633 if(
s->resample_first ? !
s->rematrix : !
s->resample)
636 if(
s->int_sample_fmt ==
s->out_sample_fmt &&
s->out.planar
639 out_count=
FFMIN(out_count, in_count);
653 if(
s->resample_first){
669 if(
s->dither.method){
671 int dither_count=
FFMAX(out_count, 1<<16);
674 conv_src = &
s->dither.temp;
682 for(ch=0; ch<
s->dither.noise.ch_count; ch++)
683 if((ret=
swri_get_dither(
s,
s->dither.noise.ch[ch],
s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828U,
s->dither.noise.fmt))<0)
687 if(
s->dither.noise_pos + out_count >
s->dither.noise.count)
688 s->dither.noise_pos = 0;
691 if (
s->mix_2_1_simd) {
692 int len1= out_count&~15;
697 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);
698 if(out_count != len1)
700 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);
703 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);
706 switch(
s->int_sample_fmt) {
713 s->dither.noise_pos += out_count;
722 return !!
s->in_buffer.ch_count;
726 uint8_t *
const *out_arg,
int out_count,
727 const uint8_t *
const *in_arg,
int in_count)
736#if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
740 while(
s->drop_output > 0){
743#define MAX_DROP_STEP 16384
748 s->drop_output *= -1;
750 s->drop_output *= -1;
753 s->drop_output -= ret;
754 if (!
s->drop_output && !out_arg)
766 s->resampler->flush(
s);
767 s->resample_in_constraint = 0;
769 }
else if(!
s->in_buffer_count){
779 if(ret>0 && !
s->drop_output)
780 s->outpts += ret * (
int64_t)
s->in_sample_rate;
782 av_assert2(max_output < 0 || ret <= max_output);
796 s->in_buffer_count -= ret;
797 s->in_buffer_index += ret;
800 if(!
s->in_buffer_count)
801 s->in_buffer_index = 0;
805 size=
s->in_buffer_index +
s->in_buffer_count + in_count - out_count;
807 if(in_count > out_count) {
808 if(
size >
s->in_buffer.count
811 copy(&
s->in_buffer, &
tmp,
s->in_buffer_count);
812 s->in_buffer_index=0;
828 buf_set(&
tmp, &
s->in_buffer,
s->in_buffer_index +
s->in_buffer_count);
830 s->in_buffer_count += in_count;
833 if(ret2>0 && !
s->drop_output)
834 s->outpts += ret2 * (
int64_t)
s->in_sample_rate;
835 av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
842 s->drop_output += count;
844 if(
s->drop_output <= 0)
858#define MAX_SILENCE_STEP 16384
868 if(
s->silence.planar)
for(
i=0;
i<
s->silence.ch_count;
i++) {
869 memset(
s->silence.ch[
i],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps);
871 memset(
s->silence.ch[0],
s->silence.bps==1 ? 0x80 : 0, count*
s->silence.bps*
s->silence.ch_count);
880 if (
s->resampler &&
s->resample){
881 return s->resampler->get_delay(
s,
base);
883 return (
s->in_buffer_count*
base + (
s->in_sample_rate>>1))/
s->in_sample_rate;
894 if (
s->resampler &&
s->resample) {
895 if (!
s->resampler->get_out_samples)
897 out_samples =
s->resampler->get_out_samples(
s, in_samples);
899 out_samples =
s->in_buffer_count + in_samples;
903 if (out_samples > INT_MAX)
912 if (!
s || compensation_distance < 0)
914 if (!compensation_distance && sample_delta)
922 if (!
s->resampler->set_compensation){
925 return s->resampler->set_compensation(
s->resample, sample_delta, compensation_distance);
934 s->outpts =
s->firstpts =
pts;
936 if(
s->min_compensation >= FLT_MAX) {
942 if(
fabs(fdelta) >
s->min_compensation) {
943 if(
s->outpts ==
s->firstpts ||
fabs(fdelta) >
s->min_hard_compensation){
950 }
else if(
s->soft_compensation_duration &&
s->max_soft_compensation) {
951 int duration =
s->out_sample_rate *
s->soft_compensation_duration;
static const uint8_t channel_map[8][8]
Audio format conversion routines.
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.
void swri_audio_convert_free(AudioConvert **ctx)
Free audio sample format converter context.
int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len)
Convert between audio sample formats.
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
#define i(width, name, range_min, range_max)
#define ss(width, name, subs,...)
Public libavutil channel layout APIs header.
static __device__ float fabs(float a)
av_cold int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt)
int swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt)
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
void av_channel_layout_default(AVChannelLayout *ch_layout, int nb_channels)
Get the default channel layout for a given number of channels.
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_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
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.
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_VERBOSE
Detailed information.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
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.
AVSampleFormat
Audio sample formats.
@ AV_SAMPLE_FMT_FLTP
float, planar
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
@ AV_SAMPLE_FMT_NB
Number of sample formats. DO NOT USE if linking dynamically.
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
@ AV_SAMPLE_FMT_DBLP
double, planar
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
#define AV_NOPTS_VALUE
Undefined timestamp value.
av_cold struct SwrContext * swr_alloc(void)
Allocate SwrContext.
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map)
Set a customized input channel mapping.
#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.
int swr_inject_silence(struct SwrContext *s, int count)
Injects the specified number of silence samples.
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.
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_cold void swr_free(SwrContext **ss)
Free the given SwrContext and set the pointer to NULL.
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,...
av_cold void swr_close(SwrContext *s)
Closes the context so that swr_is_initialized() returns 0.
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 swr_is_initialized(struct SwrContext *s)
Check whether an swr context has been initialized or not.
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance)
Activate resampling compensation ("soft" compensation).
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.
av_cold int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
@ SWR_DITHER_NS
not part of API/ABI
@ SWR_ENGINE_SWR
SW Resampler.
@ SWR_ENGINE_SOXR
SoX Resampler.
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
int av_opt_set_chlayout(void *obj, const char *name, const AVChannelLayout *channel_layout, int search_flags)
common internal API header
#define attribute_align_arg
void * av_calloc(size_t nmemb, size_t size)
Memory handling functions.
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy)
av_cold int swri_rematrix_init(SwrContext *s)
av_cold void swri_rematrix_free(SwrContext *s)
const struct Resampler swri_resampler
const struct Resampler swri_soxr_resampler
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
int nb_channels
Number of channels in this layout.
uint8_t * data
samples buffer
int ch_count
number of channels
int planar
1 if planar audio, 0 otherwise
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
enum AVSampleFormat fmt
sample format
The libswresample context.
AVChannelLayout user_out_chlayout
User set output channel layout.
AudioData in
input audio data
AVChannelLayout user_in_chlayout
User set input channel layout.
float max_soft_compensation
swr maximum soft compensation in seconds over soft_compensation_duration
int out_sample_rate
output sample rate
int in_buffer_index
cached buffer position
AudioData postin
post-input audio data: used for rematrix/resample
int in_sample_rate
input sample rate
AudioData midbuf
intermediate audio data (postin/preout)
void * log_ctx
parent logging context
AudioData preout
pre-output audio data: used for rematrix/resample
AVChannelLayout out_ch_layout
output channel layout
enum AVSampleFormat in_sample_fmt
input sample format
struct AudioConvert * in_convert
input conversion context
AVChannelLayout in_ch_layout
input channel layout
enum AVSampleFormat out_sample_fmt
output sample format
static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count, AudioData *in, int in_count)
static void copy(AudioData *out, AudioData *in, int count)
static void buf_set(AudioData *out, AudioData *in, int count)
out may be equal in.
static void reversefill_audiodata(AudioData *out, uint8_t *in_arg[SWR_CH_MAX])
static void fill_audiodata(AudioData *out, uint8_t *const in_arg[SWR_CH_MAX])
static int resample(SwrContext *s, AudioData *out_param, int out_count, const AudioData *in_param, int in_count)
static void clear_context(SwrContext *s)
int swri_check_chlayout(struct SwrContext *s, const AVChannelLayout *chl, const char *name)
static void free_temp(AudioData *a)
static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt)
int swri_realloc_audio(AudioData *a, int count)
void swri_noise_shaping_int16(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
void swri_noise_shaping_int32(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
void swri_noise_shaping_float(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)
void swri_noise_shaping_double(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count)