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