2012-05-25 14:14:53 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Andrey Utkin
|
|
|
|
* Copyright (c) 2012 Stefano Sabatini
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Filter that changes number of samples on single output operation
|
|
|
|
*/
|
|
|
|
|
2012-11-13 16:16:48 +03:00
|
|
|
#include "libavutil/channel_layout.h"
|
2012-05-25 14:14:53 +03:00
|
|
|
#include "libavutil/opt.h"
|
|
|
|
#include "avfilter.h"
|
|
|
|
#include "audio.h"
|
2018-10-03 12:20:34 +02:00
|
|
|
#include "filters.h"
|
2012-06-22 15:33:09 +03:00
|
|
|
#include "internal.h"
|
2012-05-25 14:14:53 +03:00
|
|
|
#include "formats.h"
|
|
|
|
|
2017-05-12 20:00:49 +02:00
|
|
|
typedef struct ASNSContext {
|
2012-05-25 14:14:53 +03:00
|
|
|
const AVClass *class;
|
|
|
|
int nb_out_samples; ///< how many samples to output
|
|
|
|
int pad;
|
|
|
|
} ASNSContext;
|
|
|
|
|
|
|
|
#define OFFSET(x) offsetof(ASNSContext, x)
|
2012-08-13 14:40:01 +03:00
|
|
|
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
|
2012-05-25 14:14:53 +03:00
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
static const AVOption asetnsamples_options[] = {
|
2013-04-11 16:38:49 +03:00
|
|
|
{ "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 },
|
|
|
|
{ "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
|
2015-09-08 23:05:54 +02:00
|
|
|
{ "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
|
|
|
|
{ "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
|
2013-04-11 16:38:49 +03:00
|
|
|
{ NULL }
|
2012-05-25 14:14:53 +03:00
|
|
|
};
|
|
|
|
|
2012-06-22 15:33:09 +03:00
|
|
|
AVFILTER_DEFINE_CLASS(asetnsamples);
|
2012-05-25 14:14:53 +03:00
|
|
|
|
2018-10-03 12:20:34 +02:00
|
|
|
static int activate(AVFilterContext *ctx)
|
2012-05-25 14:14:53 +03:00
|
|
|
{
|
2018-10-03 12:20:34 +02:00
|
|
|
AVFilterLink *inlink = ctx->inputs[0];
|
2012-05-25 14:14:53 +03:00
|
|
|
AVFilterLink *outlink = ctx->outputs[0];
|
2018-10-03 12:20:34 +02:00
|
|
|
ASNSContext *s = ctx->priv;
|
|
|
|
AVFrame *frame = NULL, *pad_frame;
|
2012-05-25 14:14:53 +03:00
|
|
|
int ret;
|
|
|
|
|
2018-10-03 12:20:34 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
|
|
|
|
|
|
|
|
ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
|
2017-11-20 15:02:28 +02:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2018-10-03 12:20:34 +02:00
|
|
|
if (ret > 0) {
|
2022-04-12 16:41:17 +02:00
|
|
|
if (!s->pad || frame->nb_samples == s->nb_out_samples)
|
|
|
|
return ff_filter_frame(outlink, frame);
|
2018-10-03 12:20:34 +02:00
|
|
|
|
|
|
|
pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
|
2018-10-07 22:47:19 +02:00
|
|
|
if (!pad_frame) {
|
|
|
|
av_frame_free(&frame);
|
2018-10-03 12:20:34 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2018-10-07 22:47:19 +02:00
|
|
|
}
|
2018-10-03 12:20:34 +02:00
|
|
|
|
2018-12-25 23:26:18 +02:00
|
|
|
ret = av_frame_copy_props(pad_frame, frame);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_frame_free(&pad_frame);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-10-03 12:20:34 +02:00
|
|
|
av_samples_copy(pad_frame->extended_data, frame->extended_data,
|
2021-08-31 16:03:14 +02:00
|
|
|
0, 0, frame->nb_samples, frame->ch_layout.nb_channels, frame->format);
|
2018-10-03 12:20:34 +02:00
|
|
|
av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
|
2021-08-31 16:03:14 +02:00
|
|
|
s->nb_out_samples - frame->nb_samples, frame->ch_layout.nb_channels,
|
2018-10-03 12:20:34 +02:00
|
|
|
frame->format);
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return ff_filter_frame(outlink, pad_frame);
|
2013-03-13 11:55:32 +03:00
|
|
|
}
|
2012-05-25 14:14:53 +03:00
|
|
|
|
2018-10-03 12:20:34 +02:00
|
|
|
FF_FILTER_FORWARD_STATUS(inlink, outlink);
|
2022-04-12 16:41:17 +02:00
|
|
|
if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples) {
|
|
|
|
ff_filter_set_ready(ctx, 100);
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-03 12:20:34 +02:00
|
|
|
FF_FILTER_FORWARD_WANTED(outlink, inlink);
|
|
|
|
|
|
|
|
return FFERROR_NOT_READY;
|
2012-05-25 14:14:53 +03:00
|
|
|
}
|
|
|
|
|
2012-11-28 22:01:59 +03:00
|
|
|
static const AVFilterPad asetnsamples_inputs[] = {
|
|
|
|
{
|
2018-10-03 12:20:34 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const AVFilterPad asetnsamples_outputs[] = {
|
|
|
|
{
|
2018-10-03 12:20:34 +02:00
|
|
|
.name = "default",
|
|
|
|
.type = AVMEDIA_TYPE_AUDIO,
|
2012-11-28 22:01:59 +03:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-04-19 18:33:56 +02:00
|
|
|
const AVFilter ff_af_asetnsamples = {
|
2013-09-07 15:13:50 +03:00
|
|
|
.name = "asetnsamples",
|
|
|
|
.description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
|
|
|
|
.priv_size = sizeof(ASNSContext),
|
|
|
|
.priv_class = &asetnsamples_class,
|
2021-08-12 13:05:31 +02:00
|
|
|
FILTER_INPUTS(asetnsamples_inputs),
|
|
|
|
FILTER_OUTPUTS(asetnsamples_outputs),
|
2018-10-03 12:20:34 +02:00
|
|
|
.activate = activate,
|
2012-05-25 14:14:53 +03:00
|
|
|
};
|