aresample: add code to flush the internal swr buffer.
authorMichael Niedermayer <michaelni@gmx.at>
Thu, 17 May 2012 20:39:02 +0000 (22:39 +0200)
committerMichael Niedermayer <michaelni@gmx.at>
Thu, 17 May 2012 20:45:05 +0000 (22:45 +0200)
Inspired-by code from af_resample.c written by Anton Khirnov

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
libavfilter/af_aresample.c
tests/ref/acodec/g723_1
tests/ref/acodec/roqaudio

index 8e616e2a69f493660def5cb02f9153e6a6d2bbd2..0dc413dc376ad64cf29f7d04d6f28c66dd49b937 100644 (file)
@@ -36,6 +36,7 @@
 typedef struct {
     double ratio;
     struct SwrContext *swr;
+    int64_t next_pts;
 } AResampleContext;
 
 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
@@ -44,6 +45,7 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
     int ret = 0;
     char *argd = av_strdup(args);
 
+    aresample->next_pts = AV_NOPTS_VALUE;
     aresample->swr = swr_alloc();
     if (!aresample->swr)
         return AVERROR(ENOMEM);
@@ -176,15 +178,54 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamplesref
     }
 
     avfilter_copy_buffer_ref_props(outsamplesref, insamplesref);
+
     outsamplesref->audio->sample_rate = outlink->sample_rate;
     outsamplesref->audio->nb_samples  = n_out;
-    outsamplesref->pts = insamplesref->pts == AV_NOPTS_VALUE ? AV_NOPTS_VALUE :
-        av_rescale_q(insamplesref->pts, inlink->time_base, outlink->time_base);
+
+    if(insamplesref->pts != AV_NOPTS_VALUE) {
+        aresample->next_pts = insamplesref->pts;
+        outsamplesref->pts  = av_rescale_q(insamplesref->pts, inlink->time_base, outlink->time_base);
+    } else{
+        outsamplesref->pts  = AV_NOPTS_VALUE; //aresample->next_pts;
+    }
+    if(aresample->next_pts != AV_NOPTS_VALUE)
+        aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
 
     ff_filter_samples(outlink, outsamplesref);
     avfilter_unref_buffer(insamplesref);
 }
 
+static int request_frame(AVFilterLink *outlink)
+{
+    AVFilterContext *ctx = outlink->src;
+    AResampleContext *aresample = ctx->priv;
+    int ret = avfilter_request_frame(ctx->inputs[0]);
+
+    if (ret == AVERROR_EOF) {
+        AVFilterBufferRef *outsamplesref;
+        int n_out = 4096;
+
+        outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
+        if (!outsamplesref)
+            return AVERROR(ENOMEM);
+        n_out = swr_convert(aresample->swr, outsamplesref->data, n_out, 0, 0);
+        if (n_out <= 0) {
+            avfilter_unref_buffer(outsamplesref);
+            return (n_out == 0) ? AVERROR_EOF : n_out;
+        }
+
+        outsamplesref->audio->sample_rate = outlink->sample_rate;
+        outsamplesref->audio->nb_samples  = n_out;
+        outsamplesref->pts = aresample->next_pts;
+        if(aresample->next_pts != AV_NOPTS_VALUE)
+            aresample->next_pts += av_rescale_q(n_out, (AVRational){1 ,outlink->sample_rate}, outlink->time_base);
+
+        ff_filter_samples(outlink, outsamplesref);
+        return 0;
+    }
+    return ret;
+}
+
 AVFilter avfilter_af_aresample = {
     .name          = "aresample",
     .description   = NULL_IF_CONFIG_SMALL("Resample audio data."),
@@ -200,6 +241,7 @@ AVFilter avfilter_af_aresample = {
                                   { .name = NULL}},
     .outputs   = (const AVFilterPad[]) {{ .name      = "default",
                                     .config_props    = config_output,
+                                    .request_frame   = request_frame,
                                     .type            = AVMEDIA_TYPE_AUDIO, },
                                   { .name = NULL}},
 };
index 7615badf676d070a2a76986c9d6e898406f1d9ce..8ae11b85042f445b8b80da03d19cbc11a3b2c5f1 100644 (file)
@@ -1,4 +1,4 @@
-afd309546b14cff772f3f28ee650452f *./tests/data/acodec/g723_1.tco
-4800 ./tests/data/acodec/g723_1.tco
-99030194774ea673817a56f52a04843d *./tests/data/g723_1.acodec.out.wav
-stddev: 8503.56 PSNR: 17.74 MAXDIFF:26473 bytes:    96000/  1058400
+93fcff0367883ca8e75b3063c527a2ce *./tests/data/acodec/g723_1.tco
+4824 ./tests/data/acodec/g723_1.tco
+9f28820dc27cf207a15b2048789853cd *./tests/data/g723_1.acodec.out.wav
+stddev: 8502.50 PSNR: 17.74 MAXDIFF:26473 bytes:    96480/  1058400
index fe66bdec5881dcbd544e2e977639be7d18a6b39a..3e47302a26123ce0fdf6f8f96a846cb7e2083247 100644 (file)
@@ -1,4 +1,4 @@
-3fe1e3c0feeb3963685e07c75d136ed0 *./tests/data/acodec/roqaudio.roq
+c8ff13cf7ebece23af76502f5785202e *./tests/data/acodec/roqaudio.roq
 265992 ./tests/data/acodec/roqaudio.roq
-f27d1906e28e80f0955b75cc4ffe3601 *./tests/data/roqaudio.acodec.out.wav
-stddev: 4610.92 PSNR: 23.05 MAXDIFF:43883 bytes:  1058336/  1058400
+709fd60aea880c73b375094ab5307c77 *./tests/data/roqaudio.acodec.out.wav
+stddev: 4610.71 PSNR: 23.05 MAXDIFF:43883 bytes:  1058400/  1058400