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 
27 #include "libavutil/audio_fifo.h"
28 #include "libavutil/avassert.h"
30 #include "libavutil/opt.h"
31 #include "avfilter.h"
32 #include "audio.h"
33 #include "internal.h"
34 #include "formats.h"
35 
36 typedef struct {
37  const AVClass *class;
38  int nb_out_samples; ///< how many samples to output
39  AVAudioFifo *fifo; ///< samples are queued here
40  int64_t next_out_pts;
41  int pad;
42 } ASNSContext;
43 
44 #define OFFSET(x) offsetof(ASNSContext, x)
45 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
46 
47 static const AVOption asetnsamples_options[] = {
48  { "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 },
49  { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
50  { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
51  { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.i64=1}, 0, 1, FLAGS },
52  { NULL }
53 };
54 
55 AVFILTER_DEFINE_CLASS(asetnsamples);
56 
57 static av_cold int init(AVFilterContext *ctx)
58 {
59  ASNSContext *asns = ctx->priv;
60 
62  av_log(ctx, AV_LOG_VERBOSE, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad);
63 
64  return 0;
65 }
66 
67 static av_cold void uninit(AVFilterContext *ctx)
68 {
69  ASNSContext *asns = ctx->priv;
70  av_audio_fifo_free(asns->fifo);
71 }
72 
73 static int config_props_output(AVFilterLink *outlink)
74 {
75  ASNSContext *asns = outlink->src->priv;
77 
78  asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples);
79  if (!asns->fifo)
80  return AVERROR(ENOMEM);
81  outlink->flags |= FF_LINK_FLAG_REQUEST_LOOP;
82 
83  return 0;
84 }
85 
86 static int push_samples(AVFilterLink *outlink)
87 {
88  ASNSContext *asns = outlink->src->priv;
89  AVFrame *outsamples = NULL;
90  int ret, nb_out_samples, nb_pad_samples;
91 
92  if (asns->pad) {
93  nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0;
94  nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo));
95  } else {
96  nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo));
97  nb_pad_samples = 0;
98  }
99 
100  if (!nb_out_samples)
101  return 0;
102 
103  outsamples = ff_get_audio_buffer(outlink, nb_out_samples);
104  if (!outsamples)
105  return AVERROR(ENOMEM);
106 
107  av_audio_fifo_read(asns->fifo,
108  (void **)outsamples->extended_data, nb_out_samples);
109 
110  if (nb_pad_samples)
111  av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples,
112  nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout),
113  outlink->format);
114  outsamples->nb_samples = nb_out_samples;
115  outsamples->channel_layout = outlink->channel_layout;
116  outsamples->sample_rate = outlink->sample_rate;
117  outsamples->pts = asns->next_out_pts;
118 
119  if (asns->next_out_pts != AV_NOPTS_VALUE)
120  asns->next_out_pts += nb_out_samples;
121 
122  ret = ff_filter_frame(outlink, outsamples);
123  if (ret < 0)
124  return ret;
125  return nb_out_samples;
126 }
127 
128 static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
129 {
130  AVFilterContext *ctx = inlink->dst;
131  ASNSContext *asns = ctx->priv;
132  AVFilterLink *outlink = ctx->outputs[0];
133  int ret;
134  int nb_samples = insamples->nb_samples;
135 
136  if (av_audio_fifo_space(asns->fifo) < nb_samples) {
137  av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples);
138  ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples);
139  if (ret < 0) {
140  av_log(ctx, AV_LOG_ERROR,
141  "Stretching audio fifo failed, discarded %d samples\n", nb_samples);
142  return -1;
143  }
144  }
145  av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples);
146  if (asns->next_out_pts == AV_NOPTS_VALUE)
147  asns->next_out_pts = insamples->pts;
148  av_frame_free(&insamples);
149 
150  while (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples)
151  push_samples(outlink);
152  return 0;
153 }
154 
155 static int request_frame(AVFilterLink *outlink)
156 {
157  AVFilterLink *inlink = outlink->src->inputs[0];
158  int ret;
159 
160  ret = ff_request_frame(inlink);
161  if (ret == AVERROR_EOF) {
162  ret = push_samples(outlink);
163  return ret < 0 ? ret : ret > 0 ? 0 : AVERROR_EOF;
164  }
165 
166  return ret;
167 }
168 
170  {
171  .name = "default",
172  .type = AVMEDIA_TYPE_AUDIO,
173  .filter_frame = filter_frame,
174  .needs_writable = 1,
175  },
176  { NULL }
177 };
178 
180  {
181  .name = "default",
182  .type = AVMEDIA_TYPE_AUDIO,
183  .request_frame = request_frame,
184  .config_props = config_props_output,
185  },
186  { NULL }
187 };
188 
190  .name = "asetnsamples",
191  .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
192  .priv_size = sizeof(ASNSContext),
193  .init = init,
194  .uninit = uninit,
195  .inputs = asetnsamples_inputs,
196  .outputs = asetnsamples_outputs,
197  .priv_class = &asetnsamples_class,
198 };
int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
Write data to an AVAudioFifo.
Definition: audio_fifo.c:113
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
static av_cold int init(AVFilterContext *ctx)
static int config_props_output(AVFilterLink *outlink)
AVOption.
Definition: opt.h:251
static const AVOption asetnsamples_options[]
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:117
external API header
AVFilter avfilter_af_asetnsamples
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
Reallocate an AVAudioFifo.
Definition: audio_fifo.c:97
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
Definition: audio_fifo.c:45
static av_cold void uninit(AVFilterContext *ctx)
const char * name
Pad name.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
Definition: audio_fifo.c:139
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:532
it can be given away to ff_start_frame *A reference passed to ff_filter_frame(or the deprecated ff_start_frame) is given away and must no longer be used.*A reference created with avfilter_ref_buffer belongs to the code that created it.*A reference obtained with ff_get_video_buffer or ff_get_audio_buffer belongs to the code that requested it.*A reference given as return value by the get_video_buffer or get_audio_buffer method is given away and must no longer be used.Link reference fields---------------------The AVFilterLink structure has a few AVFilterBufferRef fields.The cur_buf and out_buf were used with the deprecated start_frame/draw_slice/end_frame API and should no longer be used.src_buf
#define av_cold
Definition: attributes.h:78
AVOptions.
static const AVFilterPad asetnsamples_outputs[]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:159
#define AVERROR_EOF
End of file.
Definition: error.h:55
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
Definition: audio_fifo.c:186
static const AVFilterPad asetnsamples_inputs[]
A filter pad used for either input or output.
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
Definition: audio_fifo.c:60
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:84
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
void * priv
private data for use by the filter
Definition: avfilter.h:545
#define OFFSET(x)
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
static int filter_frame(AVFilterLink *inlink, AVFrame *insamples)
#define FLAGS
uint64_t channel_layout
Channel layout of the audio data.
Definition: frame.h:331
#define AV_LOG_VERBOSE
Definition: log.h:157
audio channel layout utility functions
#define FFMIN(a, b)
Definition: common.h:58
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:249
ret
Definition: avfilter.c:821
int64_t next_out_pts
static int request_frame(AVFilterLink *outlink)
int av_audio_fifo_space(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for writing.
Definition: audio_fifo.c:191
NULL
Definition: eval.c:55
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
Describe the class of an AVClass context structure.
Definition: log.h:50
int sample_rate
Sample rate of the audio data.
Definition: frame.h:326
Filter definition.
Definition: avfilter.h:436
int nb_out_samples
how many samples to output
AVAudioFifo * fifo
samples are queued here
const char * name
filter name
Definition: avfilter.h:437
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:539
static int push_samples(AVFilterLink *outlink)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
AVFILTER_DEFINE_CLASS(asetnsamples)
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:108
Audio FIFO Buffer.
An instance of a filter.
Definition: avfilter.h:524
int ff_request_frame(AVFilterLink *link)
Request an input frame from the filter at the other end of the link.
Definition: avfilter.c:319
int nb_channels
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
Frame requests may need to loop in order to be fulfilled.
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 inputs
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:127
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:190