Skip to content
Permalink
Browse files

Patch to use swresample instead of avresample.

  • Loading branch information
l33tmeatwad committed Jun 20, 2018
1 parent 92f9bae commit 7d583f07335b098c8b085e7ba039cf1d15678581
Binary file not shown.
2 AviSynth/LSMASHSourceVCX.vcxproj 100644 → 100755
@@ -125,6 +125,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<TargetMachine>MachineX86</TargetMachine>
<ImageHasSafeExceptionHandlers />
<AdditionalDependencies>bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@@ -145,6 +146,7 @@
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<ImageHasSafeExceptionHandlers />
<AdditionalDependencies>bcrypt.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
28 AviSynth/audio_output.cpp 100644 → 100755
@@ -26,7 +26,7 @@
extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavresample/avresample.h>
#include <libswresample/swresample.h>
#include <libavutil/opt.h>
}

@@ -96,19 +96,19 @@ void as_setup_audio_rendering
int output_channels = av_get_channel_layout_nb_channels( aohp->output_channel_layout );
aohp->output_block_align = (output_channels * aohp->output_bits_per_sample) / 8;
/* Set up resampler. */
AVAudioResampleContext *avr_ctx = aohp->avr_ctx;
avr_ctx = avresample_alloc_context();
if( !avr_ctx )
env->ThrowError( "%s: failed to avresample_alloc_context.", filter_name );
aohp->avr_ctx = avr_ctx;
av_opt_set_int( avr_ctx, "in_channel_layout", ctx->channel_layout, 0 );
av_opt_set_int( avr_ctx, "in_sample_fmt", ctx->sample_fmt, 0 );
av_opt_set_int( avr_ctx, "in_sample_rate", ctx->sample_rate, 0 );
av_opt_set_int( avr_ctx, "out_channel_layout", aohp->output_channel_layout, 0 );
av_opt_set_int( avr_ctx, "out_sample_fmt", aohp->output_sample_format, 0 );
av_opt_set_int( avr_ctx, "out_sample_rate", aohp->output_sample_rate, 0 );
av_opt_set_int( avr_ctx, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0 );
if( avresample_open( avr_ctx ) < 0 )
SwrContext *swr_ctx = aohp->swr_ctx;
swr_ctx = swr_alloc();
if( !swr_ctx )
env->ThrowError( "%s: failed to swr_alloc.", filter_name );
aohp->swr_ctx = swr_ctx;
av_opt_set_int( swr_ctx, "in_channel_layout", ctx->channel_layout, 0 );
av_opt_set_int( swr_ctx, "in_sample_fmt", ctx->sample_fmt, 0 );
av_opt_set_int( swr_ctx, "in_sample_rate", ctx->sample_rate, 0 );
av_opt_set_int( swr_ctx, "out_channel_layout", aohp->output_channel_layout, 0 );
av_opt_set_int( swr_ctx, "out_sample_fmt", aohp->output_sample_format, 0 );
av_opt_set_int( swr_ctx, "out_sample_rate", aohp->output_sample_rate, 0 );
av_opt_set_int( swr_ctx, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0 );
if( swr_init( swr_ctx ) < 0 )
env->ThrowError( "%s: failed to open resampler.", filter_name );
/* Set up AviSynth output format. */
vi->nchannels = output_channels;
14 AviSynth/exlibs.cpp 100644 → 100755
@@ -21,13 +21,13 @@
/* This file is available under an ISC license.
* However, when distributing its binary file, it will be under LGPL or GPL. */

#pragma comment( lib, "libmingwex.a" )
#pragma comment( lib, "libgcc.a" )
//#pragma comment( lib, "libmingwex.a" )
//#pragma comment( lib, "libgcc.a" )
#pragma comment( lib, "lsmash.lib" )
#pragma comment( lib, "avutil.lib" )
#pragma comment( lib, "avcodec.lib" )
#pragma comment( lib, "avformat.lib" )
#pragma comment( lib, "swscale.lib" )
#pragma comment( lib, "avresample.lib" )
#pragma comment( lib, "libavutil.a" )
#pragma comment( lib, "libavcodec.a" )
#pragma comment( lib, "libavformat.a" )
#pragma comment( lib, "libswscale.a" )
#pragma comment( lib, "libswresample.a" )

/* Add more external libraries user-self if needed. */
2 AviSynth/libavsmash_source.cpp 100644 → 100755
@@ -33,7 +33,7 @@ extern "C"
#include <libavformat/avformat.h> /* Codec specific info importer */
#include <libavcodec/avcodec.h> /* Decoder */
#include <libswscale/swscale.h> /* Colorspace converter */
#include <libavresample/avresample.h> /* Audio resampler */
#include <libswresample/swresample.h> /* Audio resampler */
#include <libavutil/imgutils.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>
2 AviSynth/lwlibav_source.cpp 100644 → 100755
@@ -30,7 +30,7 @@ extern "C"
#include <libavformat/avformat.h> /* Codec specific info importer */
#include <libavcodec/avcodec.h> /* Decoder */
#include <libswscale/swscale.h> /* Colorspace converter */
#include <libavresample/avresample.h> /* Audio resampler */
#include <libswresample/swresample.h> /* Audio resampler */
#include <libavutil/imgutils.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>
12 common/audio_output.c 100644 → 100755
@@ -27,7 +27,7 @@ extern "C"
{
#endif /* __cplusplus */
#include <libavcodec/avcodec.h>
#include <libavresample/avresample.h>
#include <libswresample/swresample.h>
#include <libavutil/mem.h>
#ifdef __cplusplus
}
@@ -48,7 +48,7 @@ static int consume_decoded_audio_samples
)
{
/* Input */
uint8_t *in_data[AVRESAMPLE_MAX_CHANNELS];
uint8_t *in_data[32];
int decoded_data_offset = sample_offset * aohp->input_block_align;
for( int i = 0; i < aohp->input_planes; i++ )
in_data[i] = frame->extended_data[i] + decoded_data_offset;
@@ -79,7 +79,7 @@ static int consume_decoded_audio_samples
out.sample_format = aohp->output_sample_format;
out.data = resampled_buffer ? &resampled_buffer : out_data;
/* Resample */
int resampled_size = resample_audio( aohp->avr_ctx, &out, &in );
int resampled_size = resample_audio( aohp->swr_ctx, &out, &in );
if( resampled_buffer && resampled_size > 0 )
resampled_size = resample_s32_to_s24( out_data, aohp->resampled_buffer, resampled_size );
return resampled_size > 0 ? resampled_size / aohp->output_block_align : 0;
@@ -162,7 +162,7 @@ uint64_t output_pcm_samples_from_packet
{
/* Detected a change of channel layout, sample rate or sample format.
* Reconfigure audio resampler. */
if( update_resampler_configuration( aohp->avr_ctx,
if( update_resampler_configuration( aohp->swr_ctx,
aohp->output_channel_layout,
aohp->output_sample_rate,
aohp->output_sample_format,
@@ -214,6 +214,6 @@ void lw_cleanup_audio_output_handler
{
if( aohp->resampled_buffer )
av_freep( &aohp->resampled_buffer );
if( aohp->avr_ctx )
avresample_free( &aohp->avr_ctx );
if( aohp->swr_ctx )
swr_free( &aohp->swr_ctx );
}
2 common/audio_output.h 100644 → 100755
@@ -24,7 +24,7 @@

typedef struct
{
AVAudioResampleContext *avr_ctx;
SwrContext *swr_ctx;
uint8_t *resampled_buffer;
int resampled_buffer_size;
int input_planes;
4 common/libavsmash_audio.c 100644 → 100755
@@ -29,7 +29,7 @@ extern "C"
#include <lsmash.h>
#include <libavformat/avformat.h>
#include <libavcodec/avcodec.h>
#include <libavresample/avresample.h>
#include <libswresample/swresample.h>
#include <libavutil/mem.h>
#include <libavutil/opt.h>
#ifdef __cplusplus
@@ -655,7 +655,7 @@ uint64_t libavsmash_audio_get_pcm_samples
else
{
/* Seek audio stream. */
if( flush_resampler_buffers( aohp->avr_ctx ) < 0 )
if( flush_resampler_buffers( aohp->swr_ctx ) < 0 )
{
config->error = 1;
lw_log_show( &config->lh, LW_LOG_FATAL,
2 common/lwindex.c 100644 → 100755
@@ -28,7 +28,7 @@ extern "C"
#endif /* __cplusplus */
#include <libavformat/avformat.h> /* Demuxer */
#include <libavcodec/avcodec.h> /* Decoder */
#include <libavresample/avresample.h> /* Resampler/Buffer */
#include <libswresample/swresample.h> /* Resampler/Buffer */
#include <libavutil/mathematics.h> /* Timebase rescaler */
#include <libavutil/pixdesc.h>
#ifdef __cplusplus
4 common/lwlibav_audio.c 100644 → 100755
@@ -28,7 +28,7 @@ extern "C"
#endif /* __cplusplus */
#include <libavformat/avformat.h> /* Demuxer */
#include <libavcodec/avcodec.h> /* Decoder */
#include <libavresample/avresample.h> /* Resampler/Buffer */
#include <libswresample/swresample.h> /* Resampler/Buffer */
#include <libavutil/mem.h>
#include <libavutil/opt.h>
#ifdef __cplusplus
@@ -546,7 +546,7 @@ uint64_t lwlibav_audio_get_pcm_samples
retry_seek:
av_packet_unref( pkt );
/* Flush audio resampler buffers. */
if( flush_resampler_buffers( aohp->avr_ctx ) < 0 )
if( flush_resampler_buffers( aohp->swr_ctx ) < 0 )
{
adhp->error = 1;
lw_log_show( &adhp->lh, LW_LOG_FATAL,
39 common/resample.c 100644 → 100755
@@ -27,7 +27,7 @@ extern "C"
{
#endif /* __cplusplus */
#include <libavcodec/avcodec.h>
#include <libavresample/avresample.h>
#include <libswresample/swresample.h>
#include <libavutil/samplefmt.h>
#include <libavutil/opt.h>
#ifdef __cplusplus
@@ -54,27 +54,27 @@ int resample_s32_to_s24( uint8_t **out_data, uint8_t *in_data, int data_size )
return resampled_size;
}

int flush_resampler_buffers( AVAudioResampleContext *avr )
int flush_resampler_buffers(SwrContext *swr )
{
avresample_close( avr );
return avresample_open( avr ) < 0 ? -1 : 0;
swr_close( swr );
return swr_init( swr ) < 0 ? -1 : 0;
}

int update_resampler_configuration( AVAudioResampleContext *avr,
int update_resampler_configuration(SwrContext *swr,
uint64_t out_channel_layout, int out_sample_rate, enum AVSampleFormat out_sample_fmt,
uint64_t in_channel_layout, int in_sample_rate, enum AVSampleFormat in_sample_fmt,
int *input_planes, int *input_block_align )
{
/* Reopen the resampler. */
avresample_close( avr );
av_opt_set_int( avr, "in_channel_layout", in_channel_layout, 0 );
av_opt_set_int( avr, "in_sample_fmt", in_sample_fmt, 0 );
av_opt_set_int( avr, "in_sample_rate", in_sample_rate, 0 );
av_opt_set_int( avr, "out_channel_layout", out_channel_layout, 0 );
av_opt_set_int( avr, "out_sample_fmt", out_sample_fmt, 0 );
av_opt_set_int( avr, "out_sample_rate", out_sample_rate, 0 );
av_opt_set_int( avr, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0 );
if( avresample_open( avr ) < 0 )
swr_close( swr );
av_opt_set_int( swr, "in_channel_layout", in_channel_layout, 0 );
av_opt_set_int( swr, "in_sample_fmt", in_sample_fmt, 0 );
av_opt_set_int( swr, "in_sample_rate", in_sample_rate, 0 );
av_opt_set_int( swr, "out_channel_layout", out_channel_layout, 0 );
av_opt_set_int( swr, "out_sample_fmt", out_sample_fmt, 0 );
av_opt_set_int( swr, "out_sample_rate", out_sample_rate, 0 );
av_opt_set_int( swr, "internal_sample_fmt", AV_SAMPLE_FMT_FLTP, 0 );
if( swr_init( swr ) < 0 )
return -1;
/* Set up the number of planes and the block alignment of input audio frame. */
int input_channels = av_get_channel_layout_nb_channels( in_channel_layout );
@@ -91,27 +91,28 @@ int update_resampler_configuration( AVAudioResampleContext *avr,
return 0;
}

int resample_audio( AVAudioResampleContext *avr, audio_samples_t *out, audio_samples_t *in )
int resample_audio(SwrContext *swr, audio_samples_t *out, audio_samples_t *in )
{
/* Don't call this function over different block aligns. */
uint8_t *out_orig = *out->data;
int out_channels = get_channel_layout_nb_channels( out->channel_layout );
int block_align = av_get_bytes_per_sample( out->sample_format ) * out_channels;
int request_sample_count = out->sample_count;
if( avresample_available( avr ) > 0 )
if( swr_get_delay( swr, request_sample_count) > 0 )
{
int resampled_count = avresample_read( avr, out->data, request_sample_count );
int resampled_count = swr_convert( swr, out->data, request_sample_count, NULL, 0 );
if( resampled_count < 0 )
return 0;
request_sample_count -= resampled_count;
*out->data += resampled_count * block_align;
}
uint8_t **in_data = in->sample_count > 0 ? in->data : NULL;
const uint8_t **indata = const_cast<const uint8_t**>(in_data);
int in_channels = get_channel_layout_nb_channels( in->channel_layout );
int in_linesize = get_linesize( in_channels, in->sample_count, in->sample_format );
int out_linesize = get_linesize( out_channels, request_sample_count, out->sample_format );
int resampled_count = avresample_convert( avr, out->data, out_linesize, request_sample_count,
in_data, in_linesize, in->sample_count );
int resampled_count = swr_convert( swr, out->data, request_sample_count,
indata, in->sample_count );
if( resampled_count < 0 )
return 0;
*out->data += resampled_count * block_align;
6 common/resample.h 100644 → 100755
@@ -50,9 +50,9 @@ static inline int get_linesize( int channel_count, int sample_count, enum AVSamp
}

int resample_s32_to_s24( uint8_t **out_data, uint8_t *in_data, int data_size );
int flush_resampler_buffers( AVAudioResampleContext *avr );
int update_resampler_configuration( AVAudioResampleContext *avr,
int flush_resampler_buffers( SwrContext *swr );
int update_resampler_configuration( SwrContext *swr,
uint64_t out_channel_layout, int out_sample_rate, enum AVSampleFormat out_sample_fmt,
uint64_t in_channel_layout, int in_sample_rate, enum AVSampleFormat in_sample_fmt,
int *input_planes, int *input_block_align );
int resample_audio( AVAudioResampleContext *avr, audio_samples_t *out, audio_samples_t *in );
int resample_audio( SwrContext *swr, audio_samples_t *out, audio_samples_t *in );

0 comments on commit 7d583f0

Please sign in to comment.
You can’t perform that action at this time.