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>
This commit is contained in:
Michael Niedermayer 2012-05-17 22:39:02 +02:00
parent b3e1b95afa
commit 847943bc51
3 changed files with 51 additions and 9 deletions

View 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}},
};

View 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

View 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