libavfilter/af_apad.c
5eae7f8f
 /*
  * Copyright (c) 2012 Michael Niedermayer
  *
  * This file is part of FFmpeg.
  *
  * FFmpeg 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.
  *
  * FFmpeg 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 FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /**
  * @file
  * audio pad filter.
  *
  * Based on af_aresample.c
  */
 
 #include "libavutil/avstring.h"
 #include "libavutil/channel_layout.h"
 #include "libavutil/opt.h"
 #include "libavutil/samplefmt.h"
 #include "libavutil/avassert.h"
 #include "avfilter.h"
 #include "audio.h"
 #include "internal.h"
 
ed93ed5e
 typedef struct APadContext {
b7e085a2
     const AVClass *class;
5eae7f8f
     int64_t next_pts;
b7e085a2
 
     int packet_size;
aade9884
     int64_t pad_len, pad_len_left;
     int64_t whole_len, whole_len_left;
5eae7f8f
 } APadContext;
 
b7e085a2
 #define OFFSET(x) offsetof(APadContext, x)
 #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
 
 static const AVOption apad_options[] = {
aade9884
     { "packet_size", "set silence packet size",                                  OFFSET(packet_size), AV_OPT_TYPE_INT,   { .i64 = 4096 }, 0, INT_MAX, A },
     { "pad_len",     "set number of samples of silence to add",                  OFFSET(pad_len),     AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
     { "whole_len",   "set minimum target number of samples in the audio stream", OFFSET(whole_len),   AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A },
b211607b
     { NULL }
b7e085a2
 };
 
 AVFILTER_DEFINE_CLASS(apad);
 
fd6228e6
 static av_cold int init(AVFilterContext *ctx)
5eae7f8f
 {
47b41feb
     APadContext *s = ctx->priv;
5eae7f8f
 
47b41feb
     s->next_pts = AV_NOPTS_VALUE;
     if (s->whole_len >= 0 && s->pad_len >= 0) {
c311431e
         av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n");
         return AVERROR(EINVAL);
     }
47b41feb
     s->pad_len_left   = s->pad_len;
     s->whole_len_left = s->whole_len;
c311431e
 
5eae7f8f
     return 0;
 }
 
a05a44e2
 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
5eae7f8f
 {
     AVFilterContext *ctx = inlink->dst;
47b41feb
     APadContext *s = ctx->priv;
c311431e
 
47b41feb
     if (s->whole_len >= 0) {
         s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0);
aade9884
         av_log(ctx, AV_LOG_DEBUG,
47b41feb
                "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left);
aade9884
     }
c311431e
 
47b41feb
     s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
5eae7f8f
     return ff_filter_frame(ctx->outputs[0], frame);
 }
 
 static int request_frame(AVFilterLink *outlink)
 {
     AVFilterContext *ctx = outlink->src;
47b41feb
     APadContext *s = ctx->priv;
5eae7f8f
     int ret;
 
     ret = ff_request_frame(ctx->inputs[0]);
 
1e1015f3
     if (ret == AVERROR_EOF && !ctx->is_disabled) {
47b41feb
         int n_out = s->packet_size;
a05a44e2
         AVFrame *outsamplesref;
c311431e
 
47b41feb
         if (s->whole_len >= 0 && s->pad_len < 0) {
             s->pad_len = s->pad_len_left = s->whole_len_left;
c311431e
         }
47b41feb
         if (s->pad_len >=0 || s->whole_len >= 0) {
             n_out = FFMIN(n_out, s->pad_len_left);
             s->pad_len_left -= n_out;
aade9884
             av_log(ctx, AV_LOG_DEBUG,
47b41feb
                    "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left);
c311431e
         }
 
cb0524f7
         if (!n_out)
c311431e
             return AVERROR_EOF;
 
a05a44e2
         outsamplesref = ff_get_audio_buffer(outlink, n_out);
5eae7f8f
         if (!outsamplesref)
             return AVERROR(ENOMEM);
 
a05a44e2
         av_assert0(outsamplesref->sample_rate == outlink->sample_rate);
         av_assert0(outsamplesref->nb_samples  == n_out);
5eae7f8f
 
         av_samples_set_silence(outsamplesref->extended_data, 0,
                                n_out,
6af050d7
                                outsamplesref->channels,
5eae7f8f
                                outsamplesref->format);
 
47b41feb
         outsamplesref->pts = s->next_pts;
         if (s->next_pts != AV_NOPTS_VALUE)
             s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
5eae7f8f
 
         return ff_filter_frame(outlink, outsamplesref);
     }
     return ret;
 }
 
 static const AVFilterPad apad_inputs[] = {
     {
         .name         = "default",
         .type         = AVMEDIA_TYPE_AUDIO,
         .filter_frame = filter_frame,
     },
b211607b
     { NULL }
5eae7f8f
 };
 
 static const AVFilterPad apad_outputs[] = {
     {
         .name          = "default",
         .request_frame = request_frame,
         .type          = AVMEDIA_TYPE_AUDIO,
     },
b211607b
     { NULL }
5eae7f8f
 };
 
325f6e0a
 AVFilter ff_af_apad = {
5eae7f8f
     .name          = "apad",
     .description   = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
     .init          = init,
     .priv_size     = sizeof(APadContext),
     .inputs        = apad_inputs,
     .outputs       = apad_outputs,
b7e085a2
     .priv_class    = &apad_class,
1776177b
     .flags         = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
5eae7f8f
 };