annotate ffmpeg/libavfilter/af_asyncts.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * This file is part of Libav.
yading@10 3 *
yading@10 4 * Libav is free software; you can redistribute it and/or
yading@10 5 * modify it under the terms of the GNU Lesser General Public
yading@10 6 * License as published by the Free Software Foundation; either
yading@10 7 * version 2.1 of the License, or (at your option) any later version.
yading@10 8 *
yading@10 9 * Libav is distributed in the hope that it will be useful,
yading@10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 12 * Lesser General Public License for more details.
yading@10 13 *
yading@10 14 * You should have received a copy of the GNU Lesser General Public
yading@10 15 * License along with Libav; if not, write to the Free Software
yading@10 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 17 */
yading@10 18
yading@10 19 #include "libavresample/avresample.h"
yading@10 20 #include "libavutil/audio_fifo.h"
yading@10 21 #include "libavutil/common.h"
yading@10 22 #include "libavutil/mathematics.h"
yading@10 23 #include "libavutil/opt.h"
yading@10 24 #include "libavutil/samplefmt.h"
yading@10 25
yading@10 26 #include "audio.h"
yading@10 27 #include "avfilter.h"
yading@10 28 #include "internal.h"
yading@10 29
yading@10 30 typedef struct ASyncContext {
yading@10 31 const AVClass *class;
yading@10 32
yading@10 33 AVAudioResampleContext *avr;
yading@10 34 int64_t pts; ///< timestamp in samples of the first sample in fifo
yading@10 35 int min_delta; ///< pad/trim min threshold in samples
yading@10 36 int first_frame; ///< 1 until filter_frame() has processed at least 1 frame with a pts != AV_NOPTS_VALUE
yading@10 37 int64_t first_pts; ///< user-specified first expected pts, in samples
yading@10 38 int comp; ///< current resample compensation
yading@10 39
yading@10 40 /* options */
yading@10 41 int resample;
yading@10 42 float min_delta_sec;
yading@10 43 int max_comp;
yading@10 44
yading@10 45 /* set by filter_frame() to signal an output frame to request_frame() */
yading@10 46 int got_output;
yading@10 47 } ASyncContext;
yading@10 48
yading@10 49 #define OFFSET(x) offsetof(ASyncContext, x)
yading@10 50 #define A AV_OPT_FLAG_AUDIO_PARAM
yading@10 51 #define F AV_OPT_FLAG_FILTERING_PARAM
yading@10 52 static const AVOption asyncts_options[] = {
yading@10 53 { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, A|F },
yading@10 54 { "min_delta", "Minimum difference between timestamps and audio data "
yading@10 55 "(in seconds) to trigger padding/trimmin the data.", OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0, INT_MAX, A|F },
yading@10 56 { "max_comp", "Maximum compensation in samples per second.", OFFSET(max_comp), AV_OPT_TYPE_INT, { .i64 = 500 }, 0, INT_MAX, A|F },
yading@10 57 { "first_pts", "Assume the first pts should be this value.", OFFSET(first_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, A|F },
yading@10 58 { NULL },
yading@10 59 };
yading@10 60
yading@10 61 AVFILTER_DEFINE_CLASS(asyncts);
yading@10 62
yading@10 63 static int init(AVFilterContext *ctx)
yading@10 64 {
yading@10 65 ASyncContext *s = ctx->priv;
yading@10 66
yading@10 67 s->pts = AV_NOPTS_VALUE;
yading@10 68 s->first_frame = 1;
yading@10 69
yading@10 70 return 0;
yading@10 71 }
yading@10 72
yading@10 73 static void uninit(AVFilterContext *ctx)
yading@10 74 {
yading@10 75 ASyncContext *s = ctx->priv;
yading@10 76
yading@10 77 if (s->avr) {
yading@10 78 avresample_close(s->avr);
yading@10 79 avresample_free(&s->avr);
yading@10 80 }
yading@10 81 }
yading@10 82
yading@10 83 static int config_props(AVFilterLink *link)
yading@10 84 {
yading@10 85 ASyncContext *s = link->src->priv;
yading@10 86 int ret;
yading@10 87
yading@10 88 s->min_delta = s->min_delta_sec * link->sample_rate;
yading@10 89 link->time_base = (AVRational){1, link->sample_rate};
yading@10 90
yading@10 91 s->avr = avresample_alloc_context();
yading@10 92 if (!s->avr)
yading@10 93 return AVERROR(ENOMEM);
yading@10 94
yading@10 95 av_opt_set_int(s->avr, "in_channel_layout", link->channel_layout, 0);
yading@10 96 av_opt_set_int(s->avr, "out_channel_layout", link->channel_layout, 0);
yading@10 97 av_opt_set_int(s->avr, "in_sample_fmt", link->format, 0);
yading@10 98 av_opt_set_int(s->avr, "out_sample_fmt", link->format, 0);
yading@10 99 av_opt_set_int(s->avr, "in_sample_rate", link->sample_rate, 0);
yading@10 100 av_opt_set_int(s->avr, "out_sample_rate", link->sample_rate, 0);
yading@10 101
yading@10 102 if (s->resample)
yading@10 103 av_opt_set_int(s->avr, "force_resampling", 1, 0);
yading@10 104
yading@10 105 if ((ret = avresample_open(s->avr)) < 0)
yading@10 106 return ret;
yading@10 107
yading@10 108 return 0;
yading@10 109 }
yading@10 110
yading@10 111 /* get amount of data currently buffered, in samples */
yading@10 112 static int64_t get_delay(ASyncContext *s)
yading@10 113 {
yading@10 114 return avresample_available(s->avr) + avresample_get_delay(s->avr);
yading@10 115 }
yading@10 116
yading@10 117 static void handle_trimming(AVFilterContext *ctx)
yading@10 118 {
yading@10 119 ASyncContext *s = ctx->priv;
yading@10 120
yading@10 121 if (s->pts < s->first_pts) {
yading@10 122 int delta = FFMIN(s->first_pts - s->pts, avresample_available(s->avr));
yading@10 123 av_log(ctx, AV_LOG_VERBOSE, "Trimming %d samples from start\n",
yading@10 124 delta);
yading@10 125 avresample_read(s->avr, NULL, delta);
yading@10 126 s->pts += delta;
yading@10 127 } else if (s->first_frame)
yading@10 128 s->pts = s->first_pts;
yading@10 129 }
yading@10 130
yading@10 131 static int request_frame(AVFilterLink *link)
yading@10 132 {
yading@10 133 AVFilterContext *ctx = link->src;
yading@10 134 ASyncContext *s = ctx->priv;
yading@10 135 int ret = 0;
yading@10 136 int nb_samples;
yading@10 137
yading@10 138 s->got_output = 0;
yading@10 139 while (ret >= 0 && !s->got_output)
yading@10 140 ret = ff_request_frame(ctx->inputs[0]);
yading@10 141
yading@10 142 /* flush the fifo */
yading@10 143 if (ret == AVERROR_EOF) {
yading@10 144 if (s->first_pts != AV_NOPTS_VALUE)
yading@10 145 handle_trimming(ctx);
yading@10 146
yading@10 147 if (nb_samples = get_delay(s)) {
yading@10 148 AVFrame *buf = ff_get_audio_buffer(link, nb_samples);
yading@10 149 if (!buf)
yading@10 150 return AVERROR(ENOMEM);
yading@10 151 ret = avresample_convert(s->avr, buf->extended_data,
yading@10 152 buf->linesize[0], nb_samples, NULL, 0, 0);
yading@10 153 if (ret <= 0) {
yading@10 154 av_frame_free(&buf);
yading@10 155 return (ret < 0) ? ret : AVERROR_EOF;
yading@10 156 }
yading@10 157
yading@10 158 buf->pts = s->pts;
yading@10 159 return ff_filter_frame(link, buf);
yading@10 160 }
yading@10 161 }
yading@10 162
yading@10 163 return ret;
yading@10 164 }
yading@10 165
yading@10 166 static int write_to_fifo(ASyncContext *s, AVFrame *buf)
yading@10 167 {
yading@10 168 int ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
yading@10 169 buf->linesize[0], buf->nb_samples);
yading@10 170 av_frame_free(&buf);
yading@10 171 return ret;
yading@10 172 }
yading@10 173
yading@10 174 static int filter_frame(AVFilterLink *inlink, AVFrame *buf)
yading@10 175 {
yading@10 176 AVFilterContext *ctx = inlink->dst;
yading@10 177 ASyncContext *s = ctx->priv;
yading@10 178 AVFilterLink *outlink = ctx->outputs[0];
yading@10 179 int nb_channels = av_get_channel_layout_nb_channels(buf->channel_layout);
yading@10 180 int64_t pts = (buf->pts == AV_NOPTS_VALUE) ? buf->pts :
yading@10 181 av_rescale_q(buf->pts, inlink->time_base, outlink->time_base);
yading@10 182 int out_size, ret;
yading@10 183 int64_t delta;
yading@10 184 int64_t new_pts;
yading@10 185
yading@10 186 /* buffer data until we get the next timestamp */
yading@10 187 if (s->pts == AV_NOPTS_VALUE || pts == AV_NOPTS_VALUE) {
yading@10 188 if (pts != AV_NOPTS_VALUE) {
yading@10 189 s->pts = pts - get_delay(s);
yading@10 190 }
yading@10 191 return write_to_fifo(s, buf);
yading@10 192 }
yading@10 193
yading@10 194 if (s->first_pts != AV_NOPTS_VALUE) {
yading@10 195 handle_trimming(ctx);
yading@10 196 if (!avresample_available(s->avr))
yading@10 197 return write_to_fifo(s, buf);
yading@10 198 }
yading@10 199
yading@10 200 /* when we have two timestamps, compute how many samples would we have
yading@10 201 * to add/remove to get proper sync between data and timestamps */
yading@10 202 delta = pts - s->pts - get_delay(s);
yading@10 203 out_size = avresample_available(s->avr);
yading@10 204
yading@10 205 if (labs(delta) > s->min_delta ||
yading@10 206 (s->first_frame && delta && s->first_pts != AV_NOPTS_VALUE)) {
yading@10 207 av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
yading@10 208 out_size = av_clipl_int32((int64_t)out_size + delta);
yading@10 209 } else {
yading@10 210 if (s->resample) {
yading@10 211 // adjust the compensation if delta is non-zero
yading@10 212 int delay = get_delay(s);
yading@10 213 int comp = s->comp + av_clip(delta * inlink->sample_rate / delay,
yading@10 214 -s->max_comp, s->max_comp);
yading@10 215 if (comp != s->comp) {
yading@10 216 av_log(ctx, AV_LOG_VERBOSE, "Compensating %d samples per second.\n", comp);
yading@10 217 if (avresample_set_compensation(s->avr, comp, inlink->sample_rate) == 0) {
yading@10 218 s->comp = comp;
yading@10 219 }
yading@10 220 }
yading@10 221 }
yading@10 222 // adjust PTS to avoid monotonicity errors with input PTS jitter
yading@10 223 pts -= delta;
yading@10 224 delta = 0;
yading@10 225 }
yading@10 226
yading@10 227 if (out_size > 0) {
yading@10 228 AVFrame *buf_out = ff_get_audio_buffer(outlink, out_size);
yading@10 229 if (!buf_out) {
yading@10 230 ret = AVERROR(ENOMEM);
yading@10 231 goto fail;
yading@10 232 }
yading@10 233
yading@10 234 if (s->first_frame && delta > 0) {
yading@10 235 int ch;
yading@10 236
yading@10 237 av_samples_set_silence(buf_out->extended_data, 0, delta,
yading@10 238 nb_channels, buf->format);
yading@10 239
yading@10 240 for (ch = 0; ch < nb_channels; ch++)
yading@10 241 buf_out->extended_data[ch] += delta;
yading@10 242
yading@10 243 avresample_read(s->avr, buf_out->extended_data, out_size);
yading@10 244
yading@10 245 for (ch = 0; ch < nb_channels; ch++)
yading@10 246 buf_out->extended_data[ch] -= delta;
yading@10 247 } else {
yading@10 248 avresample_read(s->avr, buf_out->extended_data, out_size);
yading@10 249
yading@10 250 if (delta > 0) {
yading@10 251 av_samples_set_silence(buf_out->extended_data, out_size - delta,
yading@10 252 delta, nb_channels, buf->format);
yading@10 253 }
yading@10 254 }
yading@10 255 buf_out->pts = s->pts;
yading@10 256 ret = ff_filter_frame(outlink, buf_out);
yading@10 257 if (ret < 0)
yading@10 258 goto fail;
yading@10 259 s->got_output = 1;
yading@10 260 } else if (avresample_available(s->avr)) {
yading@10 261 av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
yading@10 262 "whole buffer.\n");
yading@10 263 }
yading@10 264
yading@10 265 /* drain any remaining buffered data */
yading@10 266 avresample_read(s->avr, NULL, avresample_available(s->avr));
yading@10 267
yading@10 268 new_pts = pts - avresample_get_delay(s->avr);
yading@10 269 /* check for s->pts monotonicity */
yading@10 270 if (new_pts > s->pts) {
yading@10 271 s->pts = new_pts;
yading@10 272 ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
yading@10 273 buf->linesize[0], buf->nb_samples);
yading@10 274 } else {
yading@10 275 av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
yading@10 276 "whole buffer.\n");
yading@10 277 ret = 0;
yading@10 278 }
yading@10 279
yading@10 280 s->first_frame = 0;
yading@10 281 fail:
yading@10 282 av_frame_free(&buf);
yading@10 283
yading@10 284 return ret;
yading@10 285 }
yading@10 286
yading@10 287 static const AVFilterPad avfilter_af_asyncts_inputs[] = {
yading@10 288 {
yading@10 289 .name = "default",
yading@10 290 .type = AVMEDIA_TYPE_AUDIO,
yading@10 291 .filter_frame = filter_frame
yading@10 292 },
yading@10 293 { NULL }
yading@10 294 };
yading@10 295
yading@10 296 static const AVFilterPad avfilter_af_asyncts_outputs[] = {
yading@10 297 {
yading@10 298 .name = "default",
yading@10 299 .type = AVMEDIA_TYPE_AUDIO,
yading@10 300 .config_props = config_props,
yading@10 301 .request_frame = request_frame
yading@10 302 },
yading@10 303 { NULL }
yading@10 304 };
yading@10 305
yading@10 306 AVFilter avfilter_af_asyncts = {
yading@10 307 .name = "asyncts",
yading@10 308 .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
yading@10 309
yading@10 310 .init = init,
yading@10 311 .uninit = uninit,
yading@10 312
yading@10 313 .priv_size = sizeof(ASyncContext),
yading@10 314 .priv_class = &asyncts_class,
yading@10 315
yading@10 316 .inputs = avfilter_af_asyncts_inputs,
yading@10 317 .outputs = avfilter_af_asyncts_outputs,
yading@10 318 };