[PATCH] Implement av_audio_resample_reinit() and use it in ffmpeg for resetting the audio resample context in the case it changes during encoding.

Stefano Sabatini stefano.sabatini-lala
Fri Nov 19 12:22:13 CET 2010


Related to issue https://roundup.ffmpeg.org/issue2292.
---
 ffmpeg.c              |   11 ++++++-----
 libavcodec/avcodec.h  |    8 ++++++++
 libavcodec/resample.c |   28 ++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 5 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 1eac3cd..32577ab 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -808,13 +808,14 @@ need_realloc:
     if (enc->channels != dec->channels)
         ost->audio_resample = 1;
 
-    if (ost->audio_resample && !ost->resample) {
+    if (ost->audio_resample) {
         if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
             fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n");
-        ost->resample = av_audio_resample_init(enc->channels,    dec->channels,
-                                               enc->sample_rate, dec->sample_rate,
-                                               enc->sample_fmt,  dec->sample_fmt,
-                                               16, 10, 0, 0.8);
+        av_audio_resample_reinit(&ost->resample,
+                                 enc->channels,    dec->channels,
+                                 enc->sample_rate, dec->sample_rate,
+                                 enc->sample_fmt,  dec->sample_fmt,
+                                 16, 10, 0, 0.8);
         if (!ost->resample) {
             fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
                     dec->channels, dec->sample_rate,
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index bff9477..22e3615 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3076,6 +3076,14 @@ ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
                                         int filter_length, int log2_phase_count,
                                         int linear, double cutoff);
 
+int av_audio_resample_reinit(ReSampleContext **ctx,
+                             int output_channels, int input_channels,
+                             int output_rate, int input_rate,
+                             enum AVSampleFormat sample_fmt_out,
+                             enum AVSampleFormat sample_fmt_in,
+                             int filter_length, int log2_phase_count,
+                             int linear, double cutoff);
+
 int audio_resample(ReSampleContext *s, short *output, short *input, int nb_samples);
 void audio_resample_close(ReSampleContext *s);
 
diff --git a/libavcodec/resample.c b/libavcodec/resample.c
index 2728315..f9e48b8 100644
--- a/libavcodec/resample.c
+++ b/libavcodec/resample.c
@@ -218,6 +218,34 @@ ReSampleContext *av_audio_resample_init(int output_channels, int input_channels,
     return s;
 }
 
+
+int av_audio_resample_reinit(ReSampleContext **ctx,
+                             int output_channels, int input_channels,
+                             int output_rate, int input_rate,
+                             enum AVSampleFormat sample_fmt_out,
+                             enum AVSampleFormat sample_fmt_in,
+                             int filter_length, int log2_phase_count,
+                             int linear, double cutoff)
+{
+    if (!*ctx ||
+        (*ctx)->output_channels != output_channels ||
+        (*ctx)->input_channels  != input_channels ||
+        (*ctx)->sample_fmt[0] != sample_fmt_out ||
+        (*ctx)->sample_fmt[1] != sample_fmt_in) {
+        if (*ctx)
+            audio_resample_close(*ctx);
+        *ctx = av_audio_resample_init(output_channels,  input_channels,
+                                      output_rate, input_rate,
+                                      sample_fmt_out, sample_fmt_in,
+                                      filter_length, log2_phase_count,
+                                      linear, cutoff);
+        if (!*ctx)
+            return AVERROR(EINVAL);
+    }
+
+    return 0;
+}
+
 #if FF_API_AUDIO_OLD
 ReSampleContext *audio_resample_init(int output_channels, int input_channels,
                                      int output_rate, int input_rate)
-- 
1.7.1


--Dxnq1zWXvFF0Q93v--



More information about the ffmpeg-devel mailing list