FFmpeg
Loading...
Searching...
No Matches
af_asetnsamples.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Andrey Utkin
3 * Copyright (c) 2012 Stefano Sabatini
4 *
5 * This file is part of FFmpeg.
6 *
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.
11 *
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.
16 *
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
20 */
21
22/**
23 * @file
24 * Filter that changes number of samples on single output operation
25 */
26
28#include "libavutil/opt.h"
29#include "avfilter.h"
30#include "audio.h"
31#include "filters.h"
32
33typedef struct ASNSContext {
34 const AVClass *class;
35 int nb_out_samples; ///< how many samples to output
36 int pad;
38
39#define OFFSET(x) offsetof(ASNSContext, x)
40#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
41
43 { "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 },
44 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
45 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
46 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
47 { NULL }
48};
49
51
53{
54 AVFilterLink *inlink = ctx->inputs[0];
55 AVFilterLink *outlink = ctx->outputs[0];
56 ASNSContext *s = ctx->priv;
57 AVFrame *frame = NULL, *pad_frame;
58 const int nb_samples = ctx->is_disabled ? 0 : s->nb_out_samples;
59 int ret;
60
61 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
62
63 if (!nb_samples)
65 else
66 ret = ff_inlink_consume_samples(inlink, nb_samples, nb_samples, &frame);
67 if (ret < 0)
68 return ret;
69
70 if (ret > 0) {
71 if (!s->pad || ctx->is_disabled || frame->nb_samples >= nb_samples)
72 return ff_filter_frame(outlink, frame);
73
74 pad_frame = ff_get_audio_buffer(outlink, nb_samples);
75 if (!pad_frame) {
77 return AVERROR(ENOMEM);
78 }
79
80 ret = av_frame_copy_props(pad_frame, frame);
81 if (ret < 0) {
82 av_frame_free(&pad_frame);
84 return ret;
85 }
86
87 av_samples_copy(pad_frame->extended_data, frame->extended_data,
88 0, 0, frame->nb_samples, frame->ch_layout.nb_channels, frame->format);
89 av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
90 nb_samples - frame->nb_samples, frame->ch_layout.nb_channels,
91 frame->format);
93 return ff_filter_frame(outlink, pad_frame);
94 }
95
96 FF_FILTER_FORWARD_STATUS(inlink, outlink);
97 if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples) {
99 return 0;
100 }
101 FF_FILTER_FORWARD_WANTED(outlink, inlink);
102
103 return FFERROR_NOT_READY;
104}
105
107 .p.name = "asetnsamples",
108 .p.description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
109 .p.priv_class = &asetnsamples_class,
111 .priv_size = sizeof(ASNSContext),
114 .activate = activate,
115 .process_command = ff_filter_process_command,
116};
const FFFilter ff_af_asetnsamples
static const AVOption asetnsamples_options[]
static int activate(AVFilterContext *ctx)
#define OFFSET(x)
static AVFormatContext * ctx
const AVFilterPad ff_audio_default_filterpad[1]
An AVFilterPad array whose only entry has name "default" and is of type AVMEDIA_TYPE_AUDIO.
Definition audio.c:34
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition audio.c:74
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition avfilter.c:1112
int ff_filter_process_command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Generic processing of user supplied commands that are set in the same way as the filter options.
Definition avfilter.c:950
int ff_inlink_consume_samples(AVFilterLink *link, unsigned min, unsigned max, AVFrame **rframe)
Take samples from the link's FIFO and update the link's stats.
Definition avfilter.c:1580
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition avfilter.c:232
int ff_inlink_consume_frame(AVFilterLink *link, AVFrame **rframe)
Take a frame from the link's FIFO and update the link's stats.
Definition avfilter.c:1560
int ff_inlink_queued_samples(AVFilterLink *link)
Definition avfilter.c:1535
Main libavfilter public API header.
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FLAGS
Definition cmdutils.c:596
#define NULL
Definition coverity.c:32
static AVFrame * frame
int ret
Definition ffplay.c:391
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition opt.h:258
@ AV_OPT_TYPE_BOOL
Underlying C type is int.
Definition opt.h:326
#define AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL
Same as AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC, except that the filter will have its filter_frame() c...
Definition avfilter.h:210
#define AVERROR(e)
Definition error.h:45
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:65
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition frame.c:602
int av_samples_set_silence(uint8_t *const *audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition samplefmt.c:247
int av_samples_copy(uint8_t *const *dst, uint8_t *const *src, int dst_offset, int src_offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Copy samples from src to dst.
Definition samplefmt.c:223
static int activate(AVBitStreamFilterContext *ctx)
#define FILTER_INPUTS(array)
Definition filters.h:279
#define FILTER_OUTPUTS(array)
Definition filters.h:280
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition filters.h:744
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition filters.h:716
#define FFERROR_NOT_READY
Filters implementation helper functions and internal structures.
Definition filters.h:34
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition filters.h:689
#define AVFILTER_DEFINE_CLASS(fname)
Definition filters.h:493
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:97
AVOptions.
int nb_out_samples
how many samples to output
Describe the class of an AVClass context structure.
Definition log.h:76
An instance of a filter.
Definition avfilter.h:279
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
AVOption.
Definition opt.h:428