FFmpeg  2.1.1
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 "internal.h"
24 
25 typedef struct {
26  const AVClass *class;
30 
31 #define CONTEXT ASetRateContext
32 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
33 
34 #define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
35  { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type, \
36  { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
37 
38 #define OPT_INT(name, field, def, min, max, descr, ...) \
39  OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
40 
41 static const AVOption asetrate_options[] = {
42  OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
43  OPT_INT("r", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
44  {NULL},
45 };
46 
47 AVFILTER_DEFINE_CLASS(asetrate);
48 
50 {
51  ASetRateContext *sr = ctx->priv;
52  int sample_rates[] = { sr->sample_rate, -1 };
53 
54  ff_formats_ref(ff_make_format_list(sample_rates),
55  &ctx->outputs[0]->in_samplerates);
56  return 0;
57 }
58 
59 static av_cold int config_props(AVFilterLink *outlink)
60 {
61  AVFilterContext *ctx = outlink->src;
62  ASetRateContext *sr = ctx->priv;
63  AVFilterLink *inlink = ctx->inputs[0];
64  AVRational intb = ctx->inputs[0]->time_base;
65  int inrate = inlink->sample_rate;
66 
67  if (intb.num == 1 && intb.den == inrate) {
68  outlink->time_base.num = 1;
69  outlink->time_base.den = outlink->sample_rate;
70  } else {
71  outlink->time_base = intb;
72  sr->rescale_pts = 1;
73  if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
74  av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
75  }
76  return 0;
77 }
78 
79 static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
80 {
81  AVFilterContext *ctx = inlink->dst;
82  ASetRateContext *sr = ctx->priv;
83  AVFilterLink *outlink = ctx->outputs[0];
84 
85  frame->sample_rate = outlink->sample_rate;
86  if (sr->rescale_pts)
87  frame->pts = av_rescale(frame->pts, inlink->sample_rate,
88  outlink->sample_rate);
89  return ff_filter_frame(outlink, frame);
90 }
91 
92 static const AVFilterPad asetrate_inputs[] = {
93  {
94  .name = "default",
95  .type = AVMEDIA_TYPE_AUDIO,
96  .filter_frame = filter_frame,
97  },
98  { NULL }
99 };
100 
101 static const AVFilterPad asetrate_outputs[] = {
102  {
103  .name = "default",
104  .type = AVMEDIA_TYPE_AUDIO,
105  .config_props = config_props,
106  },
107  { NULL }
108 };
109 
111  .name = "asetrate",
112  .description = NULL_IF_CONFIG_SMALL("Change the sample rate without "
113  "altering the data."),
114  .query_formats = query_formats,
115  .priv_size = sizeof(ASetRateContext),
116  .inputs = asetrate_inputs,
117  .outputs = asetrate_outputs,
118  .priv_class = &asetrate_class,
119 };
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_asetrate.c:79
static av_cold int config_props(AVFilterLink *outlink)
Definition: af_asetrate.c:59
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
AVOption.
Definition: opt.h:253
static const AVFilterPad asetrate_inputs[]
Definition: af_asetrate.c:92
const char * name
Filter name.
Definition: avfilter.h:468
void * priv
private data for use by the filter
Definition: avfilter.h:648
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:111
int num
numerator
Definition: rational.h:44
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
static const AVOption asetrate_options[]
Definition: af_asetrate.c:41
#define av_cold
Definition: avcodec.h:653
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:294
const char * name
Pad name.
Definition: internal.h:66
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1118
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:182
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
static AVFrame * frame
Definition: demuxing.c:51
A filter pad used for either input or output.
Definition: internal.h:60
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:151
static const int sample_rates[]
Definition: dcaenc.h:32
static const AVFilterPad asetrate_outputs[]
Definition: af_asetrate.c:101
int64_t av_rescale(int64_t a, int64_t b, int64_t c) av_const
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:118
static av_cold int query_formats(AVFilterContext *ctx)
Definition: af_asetrate.c:49
AVFilter avfilter_af_asetrate
Definition: af_asetrate.c:110
Main libavfilter public API header.
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:642
sample_rate
void ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:418
#define FFMAX(a, b)
Definition: avcodec.h:923
Describe the class of an AVClass context structure.
Definition: log.h:50
int sample_rate
Sample rate of the audio data.
Definition: frame.h:349
Filter definition.
Definition: avfilter.h:464
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:102
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:635
rational number numerator/denominator
Definition: rational.h:43
int den
denominator
Definition: rational.h:45
#define AVFILTER_DEFINE_CLASS(fname)
Definition: internal.h:301
#define OPT_INT(name, field, def, min, max, descr,...)
Definition: af_asetrate.c:38
An instance of a filter.
Definition: avfilter.h:627
internal API functions