FFmpeg
af_asetrate.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Nicolas George
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "formats.h"
24 #include "internal.h"
25 
26 typedef struct ASetRateContext {
27  const AVClass *class;
31 
32 #define CONTEXT ASetRateContext
33 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
34 
35 #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
36  { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
37  { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
38 
39 #define OPT_INT(name, field, def, min, max, descr, ...) \
40  OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
41 
42 static const AVOption asetrate_options[] = {
43  OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
44  OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate",),
45  {NULL},
46 };
47 
48 AVFILTER_DEFINE_CLASS(asetrate);
49 
51 {
52  ASetRateContext *sr = ctx->priv;
53  int ret, sample_rates[] = { sr->sample_rate, -1 };
54 
56  return ret;
57 
59  return ret;
60 
62  &ctx->inputs[0]->outcfg.samplerates)) < 0)
63  return ret;
64 
66  &ctx->outputs[0]->incfg.samplerates);
67 }
68 
69 static av_cold int config_props(AVFilterLink *outlink)
70 {
71  AVFilterContext *ctx = outlink->src;
72  ASetRateContext *sr = ctx->priv;
73  AVFilterLink *inlink = ctx->inputs[0];
74  AVRational intb = ctx->inputs[0]->time_base;
75  int inrate = inlink->sample_rate;
76 
77  if (intb.num == 1 && intb.den == inrate) {
78  outlink->time_base.num = 1;
79  outlink->time_base.den = outlink->sample_rate;
80  } else {
81  outlink->time_base = intb;
82  sr->rescale_pts = 1;
83  if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
84  av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
85  }
86  return 0;
87 }
88 
90 {
91  AVFilterContext *ctx = inlink->dst;
92  ASetRateContext *sr = ctx->priv;
93  AVFilterLink *outlink = ctx->outputs[0];
94 
95  frame->sample_rate = outlink->sample_rate;
96  if (sr->rescale_pts)
97  frame->pts = av_rescale(frame->pts, inlink->sample_rate,
98  outlink->sample_rate);
99  return ff_filter_frame(outlink, frame);
100 }
101 
102 static const AVFilterPad asetrate_inputs[] = {
103  {
104  .name = "default",
105  .type = AVMEDIA_TYPE_AUDIO,
106  .filter_frame = filter_frame,
107  },
108 };
109 
110 static const AVFilterPad asetrate_outputs[] = {
111  {
112  .name = "default",
113  .type = AVMEDIA_TYPE_AUDIO,
114  .config_props = config_props,
115  },
116 };
117 
119  .name = "asetrate",
120  .description = NULL_IF_CONFIG_SMALL("Change the sample rate without "
121  "altering the data."),
122  .priv_size = sizeof(ASetRateContext),
126  .priv_class = &asetrate_class,
128 };
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
asetrate_options
static const AVOption asetrate_options[]
Definition: af_asetrate.c:42
opt.h
ff_make_format_list
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:401
filter_frame
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_asetrate.c:89
config_props
static av_cold int config_props(AVFilterLink *outlink)
Definition: af_asetrate.c:69
ff_filter_frame
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:978
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
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
asetrate_outputs
static const AVFilterPad asetrate_outputs[]
Definition: af_asetrate.c:110
AVFrame::pts
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:452
AVOption
AVOption.
Definition: opt.h:251
FILTER_QUERY_FUNC
#define FILTER_QUERY_FUNC(func)
Definition: internal.h:169
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
AVFilter::name
const char * name
Filter name.
Definition: avfilter.h:170
sample_rate
sample_rate
Definition: ffmpeg_filter.c:368
query_formats
static av_cold int query_formats(AVFilterContext *ctx)
Definition: af_asetrate.c:50
formats.h
ASetRateContext::rescale_pts
int rescale_pts
Definition: af_asetrate.c:29
ASetRateContext
Definition: af_asetrate.c:26
AVRational::num
int num
Numerator.
Definition: rational.h:59
ff_all_formats
AVFilterFormats * ff_all_formats(enum AVMediaType type)
Return a list of all formats supported by FFmpeg for the given media type.
Definition: formats.c:501
AVFilterPad
A filter pad used for either input or output.
Definition: internal.h:47
AVFILTER_DEFINE_CLASS
AVFILTER_DEFINE_CLASS(asetrate)
asetrate_inputs
static const AVFilterPad asetrate_inputs[]
Definition: af_asetrate.c:102
av_cold
#define av_cold
Definition: attributes.h:90
ff_set_common_formats
int ff_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
Definition: formats.c:770
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
ff_formats_ref
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:617
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FILTER_INPUTS
#define FILTER_INPUTS(array)
Definition: internal.h:192
frame
static AVFrame * frame
Definition: demux_decode.c:54
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
ff_af_asetrate
const AVFilter ff_af_asetrate
Definition: af_asetrate.c:118
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
ff_set_common_all_channel_counts
int ff_set_common_all_channel_counts(AVFilterContext *ctx)
Equivalent to ff_set_common_channel_layouts(ctx, ff_all_channel_counts())
Definition: formats.c:742
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:106
AVFrame::sample_rate
int sample_rate
Sample rate of the audio data.
Definition: frame.h:567
sample_rates
sample_rates
Definition: ffmpeg_filter.c:368
internal.h
AVFilterPad::name
const char * name
Pad name.
Definition: internal.h:53
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVFilter
Filter definition.
Definition: avfilter.h:166
ret
ret
Definition: filter_design.txt:187
ff_all_samplerates
AVFilterFormats * ff_all_samplerates(void)
Definition: formats.c:572
AVRational::den
int den
Denominator.
Definition: rational.h:60
avfilter.h
AVFILTER_FLAG_METADATA_ONLY
#define AVFILTER_FLAG_METADATA_ONLY
The filter is a "metadata" filter - it does not modify the frame data in any way.
Definition: avfilter.h:133
AVFilterContext
An instance of a filter.
Definition: avfilter.h:397
FILTER_OUTPUTS
#define FILTER_OUTPUTS(array)
Definition: internal.h:193
ASetRateContext::sample_rate
int sample_rate
Definition: af_asetrate.c:28
OPT_INT
#define OPT_INT(name, field, def, min, max, descr,...)
Definition: af_asetrate.c:39
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27