00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "libavresample/avresample.h"
00020 #include "libavutil/audio_fifo.h"
00021 #include "libavutil/mathematics.h"
00022 #include "libavutil/opt.h"
00023 #include "libavutil/samplefmt.h"
00024
00025 #include "audio.h"
00026 #include "avfilter.h"
00027
00028 typedef struct ASyncContext {
00029 const AVClass *class;
00030
00031 AVAudioResampleContext *avr;
00032 int64_t pts;
00033 int min_delta;
00034
00035
00036 int resample;
00037 float min_delta_sec;
00038 int max_comp;
00039 } ASyncContext;
00040
00041 #define OFFSET(x) offsetof(ASyncContext, x)
00042 #define A AV_OPT_FLAG_AUDIO_PARAM
00043 static const AVOption options[] = {
00044 { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { 0 }, 0, 1, A },
00045 { "min_delta", "Minimum difference between timestamps and audio data "
00046 "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { 0.1 }, 0, INT_MAX, A },
00047 { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { 500 }, 0, INT_MAX, A },
00048 { NULL },
00049 };
00050
00051 static const AVClass async_class = {
00052 .class_name = "asyncts filter",
00053 .item_name = av_default_item_name,
00054 .option = options,
00055 .version = LIBAVUTIL_VERSION_INT,
00056 };
00057
00058 static int init(AVFilterContext *ctx, const char *args, void *opaque)
00059 {
00060 ASyncContext *s = ctx->priv;
00061 int ret;
00062
00063 s->class = &async_class;
00064 av_opt_set_defaults(s);
00065
00066 if ((ret = av_set_options_string(s, args, "=", ":")) < 0) {
00067 av_log(ctx, AV_LOG_ERROR, "Error parsing options string '%s'.\n", args);
00068 return ret;
00069 }
00070 av_opt_free(s);
00071
00072 s->pts = AV_NOPTS_VALUE;
00073
00074 return 0;
00075 }
00076
00077 static void uninit(AVFilterContext *ctx)
00078 {
00079 ASyncContext *s = ctx->priv;
00080
00081 if (s->avr) {
00082 avresample_close(s->avr);
00083 avresample_free(&s->avr);
00084 }
00085 }
00086
00087 static int config_props(AVFilterLink *link)
00088 {
00089 ASyncContext *s = link->src->priv;
00090 int ret;
00091
00092 s->min_delta = s->min_delta_sec * link->sample_rate;
00093 link->time_base = (AVRational){1, link->sample_rate};
00094
00095 s->avr = avresample_alloc_context();
00096 if (!s->avr)
00097 return AVERROR(ENOMEM);
00098
00099 av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
00100 av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
00101 av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
00102 av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
00103 av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
00104 av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
00105
00106 if (s->resample)
00107 av_opt_set_int(s->avr, "force_resampling", 1, 0);
00108
00109 if ((ret = avresample_open(s->avr)) < 0)
00110 return ret;
00111
00112 return 0;
00113 }
00114
00115 static int request_frame(AVFilterLink *link)
00116 {
00117 AVFilterContext *ctx = link->src;
00118 ASyncContext *s = ctx->priv;
00119 int ret = avfilter_request_frame(ctx->inputs[0]);
00120 int nb_samples;
00121
00122
00123 if (ret == AVERROR_EOF && (nb_samples = avresample_get_delay(s->avr))) {
00124 AVFilterBufferRef *buf = ff_get_audio_buffer(link, AV_PERM_WRITE,
00125 nb_samples);
00126 if (!buf)
00127 return AVERROR(ENOMEM);
00128 avresample_convert(s->avr, (void**)buf->extended_data, buf->linesize[0],
00129 nb_samples, NULL, 0, 0);
00130 buf->pts = s->pts;
00131 ff_filter_samples(link, buf);
00132 return 0;
00133 }
00134
00135 return ret;
00136 }
00137
00138 static void write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
00139 {
00140 avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
00141 buf->linesize[0], buf->audio->nb_samples);
00142 avfilter_unref_buffer(buf);
00143 }
00144
00145
00146 static int64_t get_delay(ASyncContext *s)
00147 {
00148 return avresample_available(s->avr) + avresample_get_delay(s->avr);
00149 }
00150
00151 static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
00152 {
00153 AVFilterContext *ctx = inlink->dst;
00154 ASyncContext *s = ctx->priv;
00155 AVFilterLink *outlink = ctx->outputs[0];
00156 int nb_channels = av_get_channel_layout_nb_channels(buf->audio->channel_layout);
00157 int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
00158 av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
00159 int out_size;
00160 int64_t delta;
00161
00162
00163 if (s->pts == AV_NOPTS_VALUE) {
00164 if (pts != AV_NOPTS_VALUE) {
00165 s->pts = pts - get_delay(s);
00166 }
00167 write_to_fifo(s, buf);
00168 return;
00169 }
00170
00171
00172 if (pts == AV_NOPTS_VALUE) {
00173 write_to_fifo(s, buf);
00174 return;
00175 }
00176
00177
00178
00179 delta = pts - s->pts - get_delay(s);
00180 out_size = avresample_available(s->avr);
00181
00182 if (labs(delta) > s->min_delta) {
00183 av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
00184 out_size += delta;
00185 } else {
00186 if (s->resample) {
00187 int comp = av_clip(delta, -s->max_comp, s->max_comp);
00188 av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
00189 avresample_set_compensation(s->avr, delta, inlink->sample_rate);
00190 }
00191 delta = 0;
00192 }
00193
00194 if (out_size > 0) {
00195 AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
00196 out_size);
00197 if (!buf_out)
00198 return;
00199
00200 avresample_read(s->avr, (void**)buf_out->extended_data, out_size);
00201 buf_out->pts = s->pts;
00202
00203 if (delta > 0) {
00204 av_samples_set_silence(buf_out->extended_data, out_size - delta,
00205 delta, nb_channels, buf->format);
00206 }
00207 ff_filter_samples(outlink, buf_out);
00208 } else {
00209 av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
00210 "whole buffer.\n");
00211 }
00212
00213
00214 avresample_read(s->avr, NULL, avresample_available(s->avr));
00215
00216 s->pts = pts - avresample_get_delay(s->avr);
00217 avresample_convert(s->avr, NULL, 0, 0, (void**)buf->extended_data,
00218 buf->linesize[0], buf->audio->nb_samples);
00219 avfilter_unref_buffer(buf);
00220 }
00221
00222 AVFilter avfilter_af_asyncts = {
00223 .name = "asyncts",
00224 .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
00225
00226 .init = init,
00227 .uninit = uninit,
00228
00229 .priv_size = sizeof(ASyncContext),
00230
00231 .inputs = (const AVFilterPad[]) {{ .name = "default",
00232 .type = AVMEDIA_TYPE_AUDIO,
00233 .filter_samples = filter_samples },
00234 { NULL }},
00235 .outputs = (const AVFilterPad[]) {{ .name = "default",
00236 .type = AVMEDIA_TYPE_AUDIO,
00237 .config_props = config_props,
00238 .request_frame = request_frame },
00239 { NULL }},
00240 };