aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/af_aresample.c
diff options
context:
space:
mode:
authorMichael Niedermayer <michaelni@gmx.at>2012-05-17 22:39:02 +0200
committerMichael Niedermayer <michaelni@gmx.at>2012-05-17 22:45:05 +0200
commit847943bc51098dbd68301099ed132cdde0f9eabf (patch)
tree690fe6fb9e6ac6f47639f957f95a46265ac1d02e /libavfilter/af_aresample.c
parentb3e1b95afaff7ed53baef2d193e0968cade7e94b (diff)
downloadffmpeg-847943bc51098dbd68301099ed132cdde0f9eabf.tar.gz
aresample: add code to flush the internal swr buffer.
Inspired-by code from af_resample.c written by Anton Khirnov Signed-off-by: Michael Niedermayer <michaelni@gmx.at>
Diffstat (limited to 'libavfilter/af_aresample.c')
-rw-r--r--libavfilter/af_aresample.c46
1 files changed, 44 insertions, 2 deletions
diff --git a/libavfilter/af_aresample.c b/libavfilter/af_aresample.c
index 8e616e2a69..0dc413dc37 100644
--- a/libavfilter/af_aresample.c
+++ b/libavfilter/af_aresample.c
@@ -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}},
};