]> git.sesse.net Git - ffmpeg/blobdiff - libavfilter/af_asyncts.c
cosmetics: Fix dropable --> droppable typo
[ffmpeg] / libavfilter / af_asyncts.c
index 097bc6045fdf950c47e47656b43f08a3a2a8ac2d..5d009f020b60b8e550f313ee6a2aeb9dad9c879c 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "libavresample/avresample.h"
 #include "libavutil/audio_fifo.h"
+#include "libavutil/common.h"
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
@@ -38,17 +39,18 @@ typedef struct ASyncContext {
     float min_delta_sec;
     int max_comp;
 
-    /* set by filter_samples() to signal an output frame to request_frame() */
+    /* set by filter_frame() to signal an output frame to request_frame() */
     int got_output;
 } 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 },
+    { "compensate", "Stretch/squeeze the data to make it match the timestamps", OFFSET(resample),      AV_OPT_TYPE_INT,   { .i64 = 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 },
+                    "(in seconds) to trigger padding/trimmin the data.",        OFFSET(min_delta_sec), AV_OPT_TYPE_FLOAT, { .dbl = 0.1 }, 0, INT_MAX, A },
+    { "max_comp",   "Maximum compensation in samples per second.",              OFFSET(max_comp),      AV_OPT_TYPE_INT,   { .i64 = 500 }, 0, INT_MAX, A },
+    { "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 },
     { NULL },
 };
 
@@ -73,8 +75,6 @@ static int init(AVFilterContext *ctx, const char *args)
     }
     av_opt_free(s);
 
-    s->pts = AV_NOPTS_VALUE;
-
     return 0;
 }
 
@@ -133,21 +133,26 @@ static int request_frame(AVFilterLink *link)
                                                      nb_samples);
         if (!buf)
             return AVERROR(ENOMEM);
-        avresample_convert(s->avr, (void**)buf->extended_data, buf->linesize[0],
-                           nb_samples, NULL, 0, 0);
+        ret = avresample_convert(s->avr, buf->extended_data,
+                                 buf->linesize[0], nb_samples, NULL, 0, 0);
+        if (ret <= 0) {
+            avfilter_unref_bufferp(&buf);
+            return (ret < 0) ? ret : AVERROR_EOF;
+        }
+
         buf->pts = s->pts;
-        ff_filter_samples(link, buf);
-        return 0;
+        return ff_filter_frame(link, buf);
     }
 
     return ret;
 }
 
-static void write_to_fifo(ASyncContext *s, AVFilterBufferRef *buf)
+static int 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);
+    int ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
+                                 buf->linesize[0], buf->audio->nb_samples);
     avfilter_unref_buffer(buf);
+    return ret;
 }
 
 /* get amount of data currently buffered, in samples */
@@ -156,7 +161,7 @@ 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)
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *buf)
 {
     AVFilterContext  *ctx = inlink->dst;
     ASyncContext       *s = ctx->priv;
@@ -164,7 +169,7 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
     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;
+    int out_size, ret;
     int64_t delta;
 
     /* buffer data until we get the first timestamp */
@@ -172,14 +177,12 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
         if (pts != AV_NOPTS_VALUE) {
             s->pts = pts - get_delay(s);
         }
-        write_to_fifo(s, buf);
-        return;
+        return write_to_fifo(s, buf);
     }
 
     /* now wait for the next timestamp */
     if (pts == AV_NOPTS_VALUE) {
-        write_to_fifo(s, buf);
-        return;
+        return write_to_fifo(s, buf);
     }
 
     /* when we have two timestamps, compute how many samples would we have
@@ -189,7 +192,7 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
 
     if (labs(delta) > s->min_delta) {
         av_log(ctx, AV_LOG_VERBOSE, "Discontinuity - %"PRId64" samples.\n", delta);
-        out_size += delta;
+        out_size = av_clipl_int32((int64_t)out_size + delta);
     } else {
         if (s->resample) {
             int comp = av_clip(delta, -s->max_comp, s->max_comp);
@@ -202,17 +205,21 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
     if (out_size > 0) {
         AVFilterBufferRef *buf_out = ff_get_audio_buffer(outlink, AV_PERM_WRITE,
                                                          out_size);
-        if (!buf_out)
-            return;
+        if (!buf_out) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
 
-        avresample_read(s->avr, (void**)buf_out->extended_data, out_size);
+        avresample_read(s->avr, 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);
+        ret = ff_filter_frame(outlink, buf_out);
+        if (ret < 0)
+            goto fail;
         s->got_output = 1;
     } else {
         av_log(ctx, AV_LOG_WARNING, "Non-monotonous timestamps, dropping "
@@ -223,11 +230,34 @@ static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *buf)
     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);
+    ret = avresample_convert(s->avr, NULL, 0, 0, buf->extended_data,
+                             buf->linesize[0], buf->audio->nb_samples);
+
+fail:
     avfilter_unref_buffer(buf);
+
+    return ret;
 }
 
+static const AVFilterPad avfilter_af_asyncts_inputs[] = {
+    {
+        .name           = "default",
+        .type           = AVMEDIA_TYPE_AUDIO,
+        .filter_frame   = filter_frame,
+    },
+    { NULL }
+};
+
+static const AVFilterPad avfilter_af_asyncts_outputs[] = {
+    {
+        .name          = "default",
+        .type          = AVMEDIA_TYPE_AUDIO,
+        .config_props  = config_props,
+        .request_frame = request_frame
+    },
+    { NULL }
+};
+
 AVFilter avfilter_af_asyncts = {
     .name        = "asyncts",
     .description = NULL_IF_CONFIG_SMALL("Sync audio data to timestamps"),
@@ -237,13 +267,6 @@ AVFilter avfilter_af_asyncts = {
 
     .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 }},
+    .inputs      = avfilter_af_asyncts_inputs,
+    .outputs     = avfilter_af_asyncts_outputs,
 };