2 * Copyright (c) 2012 Andrey Utkin
3 * Copyright (c) 2012 Stefano Sabatini
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Filter that changes number of samples on single output operation
27 #include "libavutil/avassert.h"
28 #include "libavutil/channel_layout.h"
29 #include "libavutil/opt.h"
36 typedef struct ASNSContext {
38 int nb_out_samples; ///< how many samples to output
42 #define OFFSET(x) offsetof(ASNSContext, x)
43 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
45 static const AVOption asetnsamples_options[] = {
46 { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
47 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
48 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
49 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
53 AVFILTER_DEFINE_CLASS(asetnsamples);
55 static int activate(AVFilterContext *ctx)
57 AVFilterLink *inlink = ctx->inputs[0];
58 AVFilterLink *outlink = ctx->outputs[0];
59 ASNSContext *s = ctx->priv;
60 AVFrame *frame = NULL, *pad_frame;
63 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
65 ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
70 if (!s->pad || frame->nb_samples == s->nb_out_samples) {
71 ret = ff_filter_frame(outlink, frame);
72 if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples)
73 ff_filter_set_ready(ctx, 100);
77 pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
79 av_frame_free(&frame);
80 return AVERROR(ENOMEM);
83 ret = av_frame_copy_props(pad_frame, frame);
85 av_frame_free(&pad_frame);
86 av_frame_free(&frame);
90 av_samples_copy(pad_frame->extended_data, frame->extended_data,
91 0, 0, frame->nb_samples, frame->channels, frame->format);
92 av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
93 s->nb_out_samples - frame->nb_samples, frame->channels,
95 av_frame_free(&frame);
96 return ff_filter_frame(outlink, pad_frame);
99 FF_FILTER_FORWARD_STATUS(inlink, outlink);
100 FF_FILTER_FORWARD_WANTED(outlink, inlink);
102 return FFERROR_NOT_READY;
105 static const AVFilterPad asetnsamples_inputs[] = {
108 .type = AVMEDIA_TYPE_AUDIO,
113 static const AVFilterPad asetnsamples_outputs[] = {
116 .type = AVMEDIA_TYPE_AUDIO,
121 AVFilter ff_af_asetnsamples = {
122 .name = "asetnsamples",
123 .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
124 .priv_size = sizeof(ASNSContext),
125 .priv_class = &asetnsamples_class,
126 .inputs = asetnsamples_inputs,
127 .outputs = asetnsamples_outputs,
128 .activate = activate,