From 4c66c4071830e74afa1aea3df52059ab163c1ddb Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 4 May 2012 18:57:04 +0200 Subject: lavfi: add an audio buffer source. --- libavfilter/allfilters.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'libavfilter/allfilters.c') diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 66d890f161..25cd8222c3 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -95,6 +95,10 @@ void avfilter_register_all(void) extern AVFilter avfilter_vsrc_buffer; avfilter_register(&avfilter_vsrc_buffer); } + { + extern AVFilter avfilter_asrc_abuffer; + avfilter_register(&avfilter_asrc_abuffer); + } { extern AVFilter avfilter_vsink_buffer; avfilter_register(&avfilter_vsink_buffer); -- cgit v1.2.3 From a2cd9be212fca02dd3d6ee65cb6ab9f84c5e28e5 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Fri, 4 May 2012 19:22:38 +0200 Subject: lavfi: add an audio buffer sink. --- doc/filters.texi | 7 ++++ libavfilter/allfilters.c | 4 ++ libavfilter/buffersink.c | 102 +++++++++++++++++++++++++++++++++++++++++++++-- libavfilter/buffersink.h | 21 +++++++++- 4 files changed, 129 insertions(+), 5 deletions(-) (limited to 'libavfilter/allfilters.c') diff --git a/doc/filters.texi b/doc/filters.texi index 8d4242607a..0314bfaf20 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -191,6 +191,13 @@ Null audio sink, do absolutely nothing with the input audio. It is mainly useful as a template and to be employed in analysis / debugging tools. +@section abuffersink +This sink is intended for programmatic use. Frames that arrive on this sink can +be retrieved by the calling program using the interface defined in +@file{libavfilter/buffersink.h}. + +This filter accepts no parameters. + @c man end AUDIO SINKS @chapter Video Filters diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 25cd8222c3..c84b3f2587 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -103,6 +103,10 @@ void avfilter_register_all(void) extern AVFilter avfilter_vsink_buffer; avfilter_register(&avfilter_vsink_buffer); } + { + extern AVFilter avfilter_asink_abuffer; + avfilter_register(&avfilter_asink_abuffer); + } { extern AVFilter avfilter_vf_scale; avfilter_register(&avfilter_vf_scale); diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c index e4cbe3be42..8787268f4c 100644 --- a/libavfilter/buffersink.c +++ b/libavfilter/buffersink.c @@ -23,13 +23,20 @@ * buffer sink */ +#include "libavutil/audio_fifo.h" +#include "libavutil/audioconvert.h" #include "libavutil/fifo.h" +#include "libavutil/mathematics.h" +#include "audio.h" #include "avfilter.h" #include "buffersink.h" typedef struct { - AVFifoBuffer *fifo; ///< FIFO buffer of video frame references + AVFifoBuffer *fifo; ///< FIFO buffer of frame references + + AVAudioFifo *audio_fifo; ///< FIFO for audio samples + int64_t next_pts; ///< interpolating audio pts } BufferSinkContext; #define FIFO_INIT_SIZE 8 @@ -44,6 +51,9 @@ static av_cold void uninit(AVFilterContext *ctx) avfilter_unref_buffer(buf); } av_fifo_free(sink->fifo); + + if (sink->audio_fifo) + av_audio_fifo_free(sink->audio_fifo); } static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) @@ -58,9 +68,8 @@ static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) return 0; } -static void end_frame(AVFilterLink *link) +static void write_buf(AVFilterContext *ctx, AVFilterBufferRef *buf) { - AVFilterContext *ctx = link->dst; BufferSinkContext *sink = ctx->priv; if (av_fifo_space(sink->fifo) < sizeof(AVFilterBufferRef *) && @@ -69,10 +78,20 @@ static void end_frame(AVFilterLink *link) return; } - av_fifo_generic_write(sink->fifo, &link->cur_buf, sizeof(link->cur_buf), NULL); + av_fifo_generic_write(sink->fifo, &buf, sizeof(buf), NULL); +} + +static void end_frame(AVFilterLink *link) +{ + write_buf(link->dst, link->cur_buf); link->cur_buf = NULL; } +static void filter_samples(AVFilterLink *link, AVFilterBufferRef *buf) +{ + write_buf(link->dst, buf); +} + int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf) { BufferSinkContext *sink = ctx->priv; @@ -98,6 +117,66 @@ int av_buffersink_read(AVFilterContext *ctx, AVFilterBufferRef **buf) return 0; } +static int read_from_fifo(AVFilterContext *ctx, AVFilterBufferRef **pbuf, + int nb_samples) +{ + BufferSinkContext *s = ctx->priv; + AVFilterLink *link = ctx->inputs[0]; + AVFilterBufferRef *buf; + + if (!(buf = ff_get_audio_buffer(link, AV_PERM_WRITE, nb_samples))) + return AVERROR(ENOMEM); + av_audio_fifo_read(s->audio_fifo, (void**)buf->extended_data, nb_samples); + + buf->pts = s->next_pts; + s->next_pts += av_rescale_q(nb_samples, (AVRational){1, link->sample_rate}, + link->time_base); + + *pbuf = buf; + return 0; + +} + +int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **pbuf, + int nb_samples) +{ + BufferSinkContext *s = ctx->priv; + AVFilterLink *link = ctx->inputs[0]; + int ret = 0; + + if (!s->audio_fifo) { + int nb_channels = av_get_channel_layout_nb_channels(link->channel_layout); + if (!(s->audio_fifo = av_audio_fifo_alloc(link->format, nb_channels, nb_samples))) + return AVERROR(ENOMEM); + } + + while (ret >= 0) { + AVFilterBufferRef *buf; + + if (av_audio_fifo_size(s->audio_fifo) >= nb_samples) + return read_from_fifo(ctx, pbuf, nb_samples); + + ret = av_buffersink_read(ctx, &buf); + if (ret == AVERROR_EOF && av_audio_fifo_size(s->audio_fifo)) + return read_from_fifo(ctx, pbuf, av_audio_fifo_size(s->audio_fifo)); + else if (ret < 0) + return ret; + + if (buf->pts != AV_NOPTS_VALUE) { + s->next_pts = buf->pts - + av_rescale_q(av_audio_fifo_size(s->audio_fifo), + (AVRational){ 1, link->sample_rate }, + link->time_base); + } + + ret = av_audio_fifo_write(s->audio_fifo, (void**)buf->extended_data, + buf->audio->nb_samples); + avfilter_unref_buffer(buf); + } + + return ret; +} + AVFilter avfilter_vsink_buffer = { .name = "buffersink", .description = NULL_IF_CONFIG_SMALL("Buffer video frames, and make them available to the end of the filter graph."), @@ -112,3 +191,18 @@ AVFilter avfilter_vsink_buffer = { { .name = NULL }}, .outputs = (AVFilterPad[]) {{ .name = NULL }}, }; + +AVFilter avfilter_asink_abuffer = { + .name = "abuffersink", + .description = NULL_IF_CONFIG_SMALL("Buffer audio frames, and make them available to the end of the filter graph."), + .priv_size = sizeof(BufferSinkContext), + .init = init, + .uninit = uninit, + + .inputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_samples = filter_samples, + .min_perms = AV_PERM_READ, }, + { .name = NULL }}, + .outputs = (AVFilterPad[]) {{ .name = NULL }}, +}; diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h index e579b9ad03..8f94a9c94d 100644 --- a/libavfilter/buffersink.h +++ b/libavfilter/buffersink.h @@ -29,7 +29,7 @@ /** * Get a buffer with filtered data from sink and put it in buf. * - * @param sink pointer to a context of a buffersink AVFilter. + * @param sink pointer to a context of a buffersink or abuffersink AVFilter. * @param buf pointer to the buffer will be written here if buf is non-NULL. buf * must be freed by the caller using avfilter_unref_buffer(). * Buf may also be NULL to query whether a buffer is ready to be @@ -40,4 +40,23 @@ */ int av_buffersink_read(AVFilterContext *sink, AVFilterBufferRef **buf); +/** + * Same as av_buffersink_read, but with the ability to specify the number of + * samples read. This function is less efficient than av_buffersink_read(), + * because it copies the data around. + * + * @param sink pointer to a context of the abuffersink AVFilter. + * @param buf pointer to the buffer will be written here if buf is non-NULL. buf + * must be freed by the caller using avfilter_unref_buffer(). buf + * will contain exactly nb_samples audio samples, except at the end + * of stream, when it can contain less than nb_samples. + * Buf may also be NULL to query whether a buffer is ready to be + * output. + * + * @warning do not mix this function with av_buffersink_read(). Use only one or + * the other with a single sink, not both. + */ +int av_buffersink_read_samples(AVFilterContext *ctx, AVFilterBufferRef **buf, + int nb_samples); + #endif /* AVFILTER_BUFFERSINK_H */ -- cgit v1.2.3 From fb604ae8500d4ee7de6af61387c11618b3dea25b Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sun, 6 May 2012 09:00:53 +0200 Subject: lavfi: add aformat filter Based on a patch by Mina Nagy Zaki --- doc/filters.texi | 26 +++++++++ libavfilter/Makefile | 1 + libavfilter/af_aformat.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 176 insertions(+) create mode 100644 libavfilter/af_aformat.c (limited to 'libavfilter/allfilters.c') diff --git a/doc/filters.texi b/doc/filters.texi index 0314bfaf20..f066657add 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -107,6 +107,32 @@ build. Below is a description of the currently available audio filters. +@section aformat + +Convert the input audio to one of the specified formats. The framework will +negotiate the most appropriate format to minimize conversions. + +The filter accepts the following named parameters: +@table @option + +@item sample_fmts +A comma-separated list of requested sample formats. + +@item sample_rates +A comma-separated list of requested sample rates. + +@item channel_layouts +A comma-separated list of requested channel layouts. + +@end table + +If a parameter is omitted, all values are allowed. + +For example to force the output to either unsigned 8-bit or signed 16-bit stereo: +@example +aformat=sample_fmts\=u8\,s16:channel_layouts\=stereo +@end example + @section anull Pass the audio source unchanged to the output. diff --git a/libavfilter/Makefile b/libavfilter/Makefile index 6a6bfd6811..df75bd5e74 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -22,6 +22,7 @@ OBJS = allfilters.o \ graphparser.o \ vf_scale.o \ +OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o diff --git a/libavfilter/af_aformat.c b/libavfilter/af_aformat.c new file mode 100644 index 0000000000..84442d379e --- /dev/null +++ b/libavfilter/af_aformat.c @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2011 Mina Nagy Zaki + * + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file + * format audio filter + */ + +#include "libavutil/audioconvert.h" +#include "libavutil/avstring.h" +#include "libavutil/opt.h" + +#include "audio.h" +#include "avfilter.h" +#include "formats.h" +#include "internal.h" + +typedef struct AFormatContext { + const AVClass *class; + + AVFilterFormats *formats; + AVFilterFormats *sample_rates; + AVFilterChannelLayouts *channel_layouts; + + char *formats_str; + char *sample_rates_str; + char *channel_layouts_str; +} AFormatContext; + +#define OFFSET(x) offsetof(AFormatContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM +static const AVOption options[] = { + { "sample_fmts", "A comma-separated list of sample formats.", OFFSET(formats_str), AV_OPT_TYPE_STRING, .flags = A }, + { "sample_rates", "A comma-separated list of sample rates.", OFFSET(sample_rates_str), AV_OPT_TYPE_STRING, .flags = A }, + { "channel_layouts", "A comma-separated list of channel layouts.", OFFSET(channel_layouts_str), AV_OPT_TYPE_STRING, .flags = A }, + { NULL }, +}; + +static const AVClass aformat_class = { + .class_name = "aformat filter", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +#define PARSE_FORMATS(str, type, list, add_to_list, get_fmt, none, desc) \ +do { \ + char *next, *cur = str; \ + while (cur) { \ + type fmt; \ + next = strchr(cur, ','); \ + if (next) \ + *next++ = 0; \ + \ + if ((fmt = get_fmt(cur)) == none) { \ + av_log(ctx, AV_LOG_ERROR, "Error parsing " desc ": %s.\n", cur);\ + ret = AVERROR(EINVAL); \ + goto fail; \ + } \ + add_to_list(&list, fmt); \ + \ + cur = next; \ + } \ +} while (0) + +static int get_sample_rate(const char *samplerate) +{ + int ret = strtol(samplerate, NULL, 0); + return FFMAX(ret, 0); +} + +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) +{ + AFormatContext *s = ctx->priv; + int ret; + + if (!args) { + av_log(ctx, AV_LOG_ERROR, "No parameters supplied.\n"); + return AVERROR(EINVAL); + } + + s->class = &aformat_class; + av_opt_set_defaults(s); + + if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); + return ret; + } + + PARSE_FORMATS(s->formats_str, enum AVSampleFormat, s->formats, + avfilter_add_format, av_get_sample_fmt, AV_SAMPLE_FMT_NONE, "sample format"); + PARSE_FORMATS(s->sample_rates_str, int, s->sample_rates, avfilter_add_format, + get_sample_rate, 0, "sample rate"); + PARSE_FORMATS(s->channel_layouts_str, uint64_t, s->channel_layouts, + ff_add_channel_layout, av_get_channel_layout, 0, + "channel layout"); + +fail: + av_opt_free(s); + return ret; +} + +static int query_formats(AVFilterContext *ctx) +{ + AFormatContext *s = ctx->priv; + + avfilter_set_common_formats(ctx, s->formats ? s->formats : + avfilter_all_formats(AVMEDIA_TYPE_AUDIO)); + ff_set_common_samplerates(ctx, s->sample_rates ? s->sample_rates : + ff_all_samplerates()); + ff_set_common_channel_layouts(ctx, s->channel_layouts ? s->channel_layouts : + ff_all_channel_layouts()); + + return 0; +} + +AVFilter avfilter_af_aformat = { + .name = "aformat", + .description = NULL_IF_CONFIG_SMALL("Convert the input audio to one of the specified formats."), + .init = init, + .query_formats = query_formats, + .priv_size = sizeof(AFormatContext), + + .inputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_samples = ff_null_filter_samples }, + { .name = NULL}}, + .outputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO}, + { .name = NULL}}, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index c84b3f2587..4f5f852b8b 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -34,6 +34,7 @@ void avfilter_register_all(void) return; initialized = 1; + REGISTER_FILTER (AFORMAT, aformat, af); REGISTER_FILTER (ANULL, anull, af); REGISTER_FILTER (RESAMPLE, resample, af); -- cgit v1.2.3 From 9f26421b0be2af36b5405608f4e7429b4bd7fbdb Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Tue, 8 May 2012 16:33:50 +0200 Subject: lavfi: add asyncts filter. --- doc/filters.texi | 19 ++++ libavfilter/Makefile | 2 + libavfilter/af_asyncts.c | 237 +++++++++++++++++++++++++++++++++++++++++++++++ libavfilter/allfilters.c | 1 + 4 files changed, 259 insertions(+) create mode 100644 libavfilter/af_asyncts.c (limited to 'libavfilter/allfilters.c') diff --git a/doc/filters.texi b/doc/filters.texi index f066657add..0e611d2793 100644 --- a/doc/filters.texi +++ b/doc/filters.texi @@ -137,6 +137,25 @@ aformat=sample_fmts\=u8\,s16:channel_layouts\=stereo Pass the audio source unchanged to the output. +@section asyncts +Synchronize audio data with timestamps by squeezing/stretching it and/or +dropping samples/adding silence when needed. + +The filter accepts the following named parameters: +@table @option + +@item compensate +Enable stretching/squeezing the data to make it match the timestamps. + +@item min_delta +Minimum difference between timestamps and audio data (in seconds) to trigger +adding/dropping samples. + +@item max_comp +Maximum compensation in samples per second. + +@end table + @section resample Convert the audio sample format, sample rate and channel layout. This filter is not meant to be used directly, it is inserted automatically by libavfilter diff --git a/libavfilter/Makefile b/libavfilter/Makefile index df75bd5e74..a90d8a02b1 100644 --- a/libavfilter/Makefile +++ b/libavfilter/Makefile @@ -1,5 +1,6 @@ NAME = avfilter FFLIBS = avutil swscale +FFLIBS-$(CONFIG_ASYNCTS_FILTER) += avresample FFLIBS-$(CONFIG_MOVIE_FILTER) += avformat avcodec FFLIBS-$(CONFIG_RESAMPLE_FILTER) += avresample @@ -24,6 +25,7 @@ OBJS = allfilters.o \ OBJS-$(CONFIG_AFORMAT_FILTER) += af_aformat.o OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o +OBJS-$(CONFIG_ASYNCTS_FILTER) += af_asyncts.o OBJS-$(CONFIG_RESAMPLE_FILTER) += af_resample.o OBJS-$(CONFIG_ANULLSRC_FILTER) += asrc_anullsrc.o diff --git a/libavfilter/af_asyncts.c b/libavfilter/af_asyncts.c new file mode 100644 index 0000000000..5cde0bf00a --- /dev/null +++ b/libavfilter/af_asyncts.c @@ -0,0 +1,237 @@ +/* + * This file is part of Libav. + * + * Libav is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * Libav is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Libav; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavresample/avresample.h" +#include "libavutil/audio_fifo.h" +#include "libavutil/mathematics.h" +#include "libavutil/opt.h" +#include "libavutil/samplefmt.h" + +#include "audio.h" +#include "avfilter.h" + +typedef struct ASyncContext { + const AVClass *class; + + AVAudioResampleContext *avr; + int64_t pts; ///< timestamp in samples of the first sample in fifo + int min_delta; ///< pad/trim min threshold in samples + + /* options */ + int resample; + float min_delta_sec; + int max_comp; +} ASyncContext; + +#define OFFSET(x) offsetof(ASyncContext, x) +#define A AV_OPT_FLAG_AUDIO_PARAM +static const AVOption options[] = { + { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { 0 }, 0, 1, A }, + { "min_delta", "Minimum difference between timestamps and audio data " + "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { 0.1 }, 0, INT_MAX, A }, + { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { 500 }, 0, INT_MAX, A }, + { NULL }, +}; + +static const AVClass async_class = { + .class_name = "asyncts filter", + .item_name = av_default_item_name, + .option = options, + .version = LIBAVUTIL_VERSION_INT, +}; + +static int init(AVFilterContext *ctx, const char *args, void *opaque) +{ + ASyncContext *s = ctx->priv; + int ret; + + s->class = &async_class; + av_opt_set_defaults(s); + + if ((ret = av_set_options_string(s, args, "=", ":")) < 0) { + av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args); + return ret; + } + av_opt_free(s); + + s->pts = AV_NOPTS_VALUE; + + return 0; +} + +static void uninit(AVFilterContext *ctx) +{ + ASyncContext *s = ctx->priv; + + if (s->avr) { + avresample_close(s->avr); + avresample_free(&s->avr); + } +} + +static int config_props(AVFilterLink *link) +{ + ASyncContext *s = link->src->priv; + int ret; + + s->min_delta = s->min_delta_sec * link->sample_rate; + link->time_base = (AVRational){1, link->sample_rate}; + + s->avr = avresample_alloc_context(); + if (!s->avr) + return AVERROR(ENOMEM); + + av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0); + av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0); + av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0); + av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0); + av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0); + av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0); + + if (s->resample) + av_opt_set_int(s->avr, "force_resampling", 1, 0); + + if ((ret = avresample_open(s->avr)) < 0) + return ret; + + return 0; +} + +static int request_frame(AVFilterLink *link) +{ + AVFilterContext *ctx = link->src; + ASyncContext *s = ctx->priv; + int ret = avfilter_request_frame(ctx->inputs[0]); + int nb_samples; + + /* flush the fifo */ + if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) { + AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE, + nb_samples); + if (!buf) + return AVERROR(ENOMEM); + avresample_convert(s->avr, (void**)buf->extended_data, buf->linesize[0], + nb_samples, NULL, 0, 0); + buf->pts = s->pts; + ff_filter_samples(link, buf); + return 0; + } + + return ret; +} + +static void write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf) +{ + avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data, + buf->linesize[0], buf->audio->nb_samples); + avfilter_unref_buffer(buf); +} + +/* get amount of data currently buffered, in samples */ +static int64_t get_delay(ASyncContext *s) +{ + return avresample_available(s->avr) + avresample_get_delay(s->avr); +} + +static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf) +{ + AVFilterContext *ctx = inlink->dst; + ASyncContext *s = ctx->priv; + AVFilterLink *outlink = ctx->outputs[0]; + int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout); + int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts : + av_rescale_q(buf->pts, inlink->time_base, outlink->time_base); + int out_size; + int64_t delta; + + /* buffer data until we get the first timestamp */ + if (s->pts == AV_NOPTS_VALUE) { + if (pts != AV_NOPTS_VALUE) { + s->pts = pts - get_delay(s); + } + write_to_fifo(s, buf); + return; + } + + /* now wait for the next timestamp */ + if (pts == AV_NOPTS_VALUE) { + write_to_fifo(s, buf); + return; + } + + /* when we have two timestamps, compute how many samples would we have + * to add/remove to get proper sync between data and timestamps */ + delta = pts - s->pts - get_delay(s); + out_size = avresample_available(s->avr); + + if (labs(delta) > s->min_delta) { + av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta); + out_size += delta; + } else if (s->resample) { + int comp = av_clip(delta, -s->max_comp, s->max_comp); + av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp); + avresample_set_compensation(s->avr, delta, inlink->sample_rate); + } + + if (out_size > 0) { + AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE, + out_size); + if (!buf_out) + return; + + avresample_read(s->avr, (void**)buf_out->extended_data, out_size); + buf_out->pts = s->pts; + + if (delta > 0) { + av_samples_set_silence(buf_out->extended_data, out_size - delta, + delta, nb_channels, buf->format); + } + ff_filter_samples(outlink, buf_out); + } else { + av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping " + "whole buffer.\n"); + } + + /* drain any remaining buffered data */ + avresample_read(s->avr, NULL, avresample_available(s->avr)); + + s->pts = pts - avresample_get_delay(s->avr); + avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data, + buf->linesize[0], buf->audio->nb_samples); + avfilter_unref_buffer(buf); +} + +AVFilter avfilter_af_asyncts = { + .name = "asyncts", + .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"), + + .init = init, + .uninit = uninit, + + .priv_size = sizeof(ASyncContext), + + .inputs = (const AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .filter_samples = filter_samples }, + { NULL }}, + .outputs = (const AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_AUDIO, + .config_props = config_props, + .request_frame = request_frame }, + { NULL }}, +}; diff --git a/libavfilter/allfilters.c b/libavfilter/allfilters.c index 4f5f852b8b..3fa0152d86 100644 --- a/libavfilter/allfilters.c +++ b/libavfilter/allfilters.c @@ -36,6 +36,7 @@ void avfilter_register_all(void) REGISTER_FILTER (AFORMAT, aformat, af); REGISTER_FILTER (ANULL, anull, af); + REGISTER_FILTER (ASYNCTS, asyncts, af); REGISTER_FILTER (RESAMPLE, resample, af); REGISTER_FILTER (ANULLSRC, anullsrc, asrc); -- cgit v1.2.3