FFmpeg
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 #include "internal.h"
33 #include "formats.h"
34 
35 typedef struct ASNSContext {
36  const AVClass *class;
37  int nb_out_samples; ///< how many samples to output
38  int pad;
39 } ASNSContext;
40 
41 #define OFFSET(x) offsetof(ASNSContext, x)
42 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43 
44 static const AVOption asetnsamples_options[] = {
45  { "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 },
46  { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
47  { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
48  { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
49  { NULL }
50 };
51 
52 AVFILTER_DEFINE_CLASS(asetnsamples);
53 
55 {
56  AVFilterLink *inlink = ctx->inputs[0];
57  AVFilterLink *outlink = ctx->outputs[0];
58  ASNSContext *s = ctx->priv;
59  AVFrame *frame = NULL, *pad_frame;
60  int ret;
61 
63 
64  ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
65  if (ret < 0)
66  return ret;
67 
68  if (ret > 0) {
69  if (!s->pad || frame->nb_samples == s->nb_out_samples)
70  return ff_filter_frame(outlink, frame);
71 
72  pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
73  if (!pad_frame) {
75  return AVERROR(ENOMEM);
76  }
77 
78  ret = av_frame_copy_props(pad_frame, frame);
79  if (ret < 0) {
80  av_frame_free(&pad_frame);
82  return ret;
83  }
84 
85  av_samples_copy(pad_frame->extended_data, frame->extended_data,
86  0, 0, frame->nb_samples, frame->ch_layout.nb_channels, frame->format);
87  av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
88  s->nb_out_samples - frame->nb_samples, frame->ch_layout.nb_channels,
89  frame->format);
91  return ff_filter_frame(outlink, pad_frame);
92  }
93 
95  if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples) {
97  return 0;
98  }
100 
101  return FFERROR_NOT_READY;
102 }
103 
105  {
106  .name = "default",
107  .type = AVMEDIA_TYPE_AUDIO,
108  },
109 };
110 
112  {
113  .name = "default",
114  .type = AVMEDIA_TYPE_AUDIO,
115  },
116 };
117 
119  .name = "asetnsamples",
120  .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
121  .priv_size = sizeof(ASNSContext),
122  .priv_class = &asetnsamples_class,
125  .activate = activate,
126 };
ff_get_audio_buffer
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:100
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:969
FFERROR_NOT_READY
return FFERROR_NOT_READY
Definition: filter_design.txt:204
inlink
The exact code depends on how similar the blocks are and how related they are to the and needs to apply these operations to the correct inlink or outlink if there are several Macros are available to factor that when no extra processing is inlink
Definition: filter_design.txt:212
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:99
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVOption
AVOption.
Definition: opt.h:251
asetnsamples_inputs
static const AVFilterPad asetnsamples_inputs[]
Definition: af_asetnsamples.c:104
activate
static int activate(AVFilterContext *ctx)
Definition: af_asetnsamples.c:54
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:165
FF_FILTER_FORWARD_STATUS_BACK
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
ff_af_asetnsamples
const AVFilter ff_af_asetnsamples
Definition: af_asetnsamples.c:118
formats.h
asetnsamples_options
static const AVOption asetnsamples_options[]
Definition: af_asetnsamples.c:44
ASNSContext::pad
int pad
Definition: af_asetnsamples.c:38
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:49
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asetnsamples)
s
#define s(width, name)
Definition: cbs_vp9.c:256
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
filters.h
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:194
FLAGS
#define FLAGS
Definition: af_asetnsamples.c:42
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
ff_inlink_consume_samples
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:1383
NULL
#define NULL
Definition: coverity.c:32
av_frame_copy_props
int av_frame_copy_props(AVFrame *dst, const AVFrame *src)
Copy only "metadata" fields from src to dst.
Definition: frame.c:594
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:115
ASNSContext::nb_out_samples
int nb_out_samples
how many samples to output
Definition: af_asetnsamples.c:37
FF_FILTER_FORWARD_WANTED
FF_FILTER_FORWARD_WANTED(outlink, inlink)
internal.h
ASNSContext
Definition: af_asetnsamples.c:35
av_samples_copy
int av_samples_copy(uint8_t **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:222
asetnsamples_outputs
static const AVFilterPad asetnsamples_outputs[]
Definition: af_asetnsamples.c:111
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:55
ff_inlink_queued_samples
int ff_inlink_queued_samples(AVFilterLink *link)
Definition: avfilter.c:1343
av_samples_set_silence
int av_samples_set_silence(uint8_t **audio_data, int offset, int nb_samples, int nb_channels, enum AVSampleFormat sample_fmt)
Fill an audio buffer with silence.
Definition: samplefmt.c:246
AVFilter
Filter definition.
Definition: avfilter.h:161
ret
ret
Definition: filter_design.txt:187
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
OFFSET
#define OFFSET(x)
Definition: af_asetnsamples.c:41
channel_layout.h
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Definition: opt.h:225
avfilter.h
AVFilterContext
An instance of a filter.
Definition: avfilter.h:392
audio.h
FF_FILTER_FORWARD_STATUS
FF_FILTER_FORWARD_STATUS(inlink, outlink)
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:244
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:195
ff_filter_set_ready
void ff_filter_set_ready(AVFilterContext *filter, unsigned priority)
Mark a filter ready and schedule it for activation.
Definition: avfilter.c:204