FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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/avassert.h"
29 #include "libavutil/opt.h"
30 #include "avfilter.h"
31 #include "audio.h"
32 #include "filters.h"
33 #include "internal.h"
34 #include "formats.h"
35 
36 typedef struct ASNSContext {
37  const AVClass *class;
38  int nb_out_samples; ///< how many samples to output
39  int pad;
40 } ASNSContext;
41 
42 #define OFFSET(x) offsetof(ASNSContext, x)
43 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
44 
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 },
50  { NULL }
51 };
52 
53 AVFILTER_DEFINE_CLASS(asetnsamples);
54 
56 {
57  AVFilterLink *inlink = ctx->inputs[0];
58  AVFilterLink *outlink = ctx->outputs[0];
59  ASNSContext *s = ctx->priv;
60  AVFrame *frame = NULL, *pad_frame;
61  int ret;
62 
63  FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
64 
65  ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
66  if (ret < 0)
67  return ret;
68 
69  if (ret > 0) {
70  if ((!s->pad || (s->pad && frame->nb_samples == s->nb_out_samples)))
71  return ff_filter_frame(outlink, frame);
72 
73  pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
74  if (!pad_frame) {
75  av_frame_free(&frame);
76  return AVERROR(ENOMEM);
77  }
78 
79  av_samples_copy(pad_frame->extended_data, frame->extended_data,
80  0, 0, frame->nb_samples, frame->channels, frame->format);
81  av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
82  s->nb_out_samples - frame->nb_samples, frame->channels,
83  frame->format);
84  av_frame_free(&frame);
85  return ff_filter_frame(outlink, pad_frame);
86  }
87 
88  FF_FILTER_FORWARD_STATUS(inlink, outlink);
89  FF_FILTER_FORWARD_WANTED(outlink, inlink);
90 
91  return FFERROR_NOT_READY;
92 }
93 
94 static const AVFilterPad asetnsamples_inputs[] = {
95  {
96  .name = "default",
97  .type = AVMEDIA_TYPE_AUDIO,
98  },
99  { NULL }
100 };
101 
103  {
104  .name = "default",
105  .type = AVMEDIA_TYPE_AUDIO,
106  },
107  { NULL }
108 };
109 
111  .name = "asetnsamples",
112  .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
113  .priv_size = sizeof(ASNSContext),
114  .priv_class = &asetnsamples_class,
115  .inputs = asetnsamples_inputs,
116  .outputs = asetnsamples_outputs,
117  .activate = activate,
118 };
#define NULL
Definition: coverity.c:32
This structure describes decoded (raw) audio or video data.
Definition: frame.h:226
AVOption.
Definition: opt.h:246
static const AVOption asetnsamples_options[]
AVFilter ff_af_asetnsamples
Main libavfilter public API header.
#define FFERROR_NOT_READY
Filters implementation helper functions.
Definition: filters.h:34
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:346
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1080
AVOptions.
static const AVFilterPad asetnsamples_outputs[]
static AVFrame * frame
#define FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink)
Forward the status on an output link to an input link.
Definition: filters.h:199
static const AVFilterPad asetnsamples_inputs[]
A filter pad used for either input or output.
Definition: internal.h:54
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:237
AVFrame * ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
Request an audio samples buffer with a specific set of permissions.
Definition: audio.c:86
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:202
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
void * priv
private data for use by the filter
Definition: avfilter.h:353
#define OFFSET(x)
simple assert() macros that are a bit more flexible than ISO C assert().
static int activate(AVFilterContext *ctx)
#define FLAGS
#define FF_FILTER_FORWARD_WANTED(outlink, inlink)
Forward the frame_wanted_out flag from an output link to an input link.
Definition: filters.h:254
int channels
number of audio channels, only used for audio.
Definition: frame.h:531
audio channel layout utility functions
AVFormatContext * ctx
Definition: movenc.c:48
#define s(width, name)
Definition: cbs_vp9.c:257
static const AVFilterPad inputs[]
Definition: af_acontrast.c:193
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
Definition: frame.h:299
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:1500
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:213
Describe the class of an AVClass context structure.
Definition: log.h:67
Filter definition.
Definition: avfilter.h:144
int nb_out_samples
how many samples to output
const char * name
Filter name.
Definition: avfilter.h:148
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:350
#define FF_FILTER_FORWARD_STATUS(inlink, outlink)
Acknowledge the status on an input link and forward it to an output link.
Definition: filters.h:226
AVFILTER_DEFINE_CLASS(asetnsamples)
An instance of a filter.
Definition: avfilter.h:338
internal API functions
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:273
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:292