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
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  */
21 #include "libavutil/opt.h"
22 #include "avfilter.h"
23 #include "internal.h"
25 typedef struct ASetRateContext {
26  const AVClass *class;
31 #define CONTEXT ASetRateContext
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__ }
38 #define OPT_INT(name, field, def, min, max, descr, ...) \
39  OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
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 };
50 {
51  ASetRateContext *sr = ctx->priv;
52  int sample_rates[] = { sr->sample_rate, -1 };
54  return ff_formats_ref(ff_make_format_list(sample_rates),
55  &ctx->outputs[0]->incfg.samplerates);
56 }
58 static av_cold int config_props(AVFilterLink *outlink)
59 {
60  AVFilterContext *ctx = outlink->src;
61  ASetRateContext *sr = ctx->priv;
62  AVFilterLink *inlink = ctx->inputs[0];
63  AVRational intb = ctx->inputs[0]->time_base;
64  int inrate = inlink->sample_rate;
66  if (intb.num == 1 && intb.den == inrate) {
67  outlink->time_base.num = 1;
68  outlink->time_base.den = outlink->sample_rate;
69  } else {
70  outlink->time_base = intb;
71  sr->rescale_pts = 1;
72  if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate))
73  av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n");
74  }
75  return 0;
76 }
79 {
80  AVFilterContext *ctx = inlink->dst;
81  ASetRateContext *sr = ctx->priv;
82  AVFilterLink *outlink = ctx->outputs[0];
84  frame->sample_rate = outlink->sample_rate;
85  if (sr->rescale_pts)
86  frame->pts = av_rescale(frame->pts, inlink->sample_rate,
87  outlink->sample_rate);
88  return ff_filter_frame(outlink, frame);
89 }
91 static const AVFilterPad asetrate_inputs[] = {
92  {
93  .name = "default",
95  .filter_frame = filter_frame,
96  },
97  { NULL }
98 };
100 static const AVFilterPad asetrate_outputs[] = {
101  {
102  .name = "default",
103  .type = AVMEDIA_TYPE_AUDIO,
104  .config_props = config_props,
105  },
106  { NULL }
107 };
110  .name = "asetrate",
111  .description = NULL_IF_CONFIG_SMALL("Change the sample rate without "
112  "altering the data."),
113  .query_formats = query_formats,
114  .priv_size = sizeof(ASetRateContext),
115  .inputs = asetrate_inputs,
116  .outputs = asetrate_outputs,
117  .priv_class = &asetrate_class,
118 };
#define NULL
Definition: coverity.c:32
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: af_asetrate.c:78
static av_cold int config_props(AVFilterLink *outlink)
Definition: af_asetrate.c:58
This structure describes decoded (raw) audio or video data.
Definition: frame.h:314
Definition: opt.h:248
static const AVFilterPad asetrate_inputs[]
Definition: af_asetrate.c:91
Something somehow does not look correct.
Definition: log.h:200
Main libavfilter public API header.
int num
Definition: rational.h:59
static const AVOption asetrate_options[]
Definition: af_asetrate.c:41
AVFilterFormats * ff_make_format_list(const int *fmts)
Create a list of supported formats.
Definition: formats.c:287
const char * name
Pad name.
Definition: internal.h:60
AVFilterLink ** inputs
array of pointers to input links
Definition: avfilter.h:349
int ff_filter_frame(AVFilterLink *link, AVFrame *frame)
Send a frame of data to the next filter.
Definition: avfilter.c:1093
#define av_cold
Definition: attributes.h:88
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
Definition: frame.h:407
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AVFilter ff_af_asetrate
Definition: af_asetrate.c:109
#define av_log(a,...)
A filter pad used for either input or output.
Definition: internal.h:54
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:115
void * priv
private data for use by the filter
Definition: avfilter.h:356
#define FFMAX(a, b)
Definition: common.h:103
static const AVFilterPad asetrate_outputs[]
Definition: af_asetrate.c:100
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
int ff_formats_ref(AVFilterFormats *f, AVFilterFormats **ref)
Add *ref as a new reference to formats.
Definition: formats.c:467
AVFormatContext * ctx
Definition: movenc.c:48
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
static av_cold int query_formats(AVFilterContext *ctx)
Definition: af_asetrate.c:49
AVFilterFormats * samplerates
Lists of supported sample rates, only for audio.
Definition: avfilter.h:450
static const AVFilterPad outputs[]
Definition: af_acontrast.c:203
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
Describe the class of an AVClass context structure.
Definition: log.h:67
int sample_rate
Sample rate of the audio data.
Definition: frame.h:486
Filter definition.
Definition: avfilter.h:145
Rational number (pair of numerator and denominator).
Definition: rational.h:58
const char * name
Filter name.
Definition: avfilter.h:149
AVFilterLink ** outputs
array of pointers to output links
Definition: avfilter.h:353
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
int den
Definition: rational.h:60
#define OPT_INT(name, field, def, min, max, descr,...)
Definition: af_asetrate.c:38
An instance of a filter.
Definition: avfilter.h:341
internal API functions