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 ASetRateContext { |
26 |
|
|
const AVClass *class; |
27 |
|
|
int sample_rate; |
28 |
|
|
int rescale_pts; |
29 |
|
|
} ASetRateContext; |
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 |
|
|
|
49 |
|
1 |
static av_cold int query_formats(AVFilterContext *ctx) |
50 |
|
|
{ |
51 |
|
1 |
ASetRateContext *sr = ctx->priv; |
52 |
|
1 |
int sample_rates[] = { sr->sample_rate, -1 }; |
53 |
|
|
|
54 |
|
1 |
return ff_formats_ref(ff_make_format_list(sample_rates), |
55 |
|
1 |
&ctx->outputs[0]->incfg.samplerates); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
1 |
static av_cold int config_props(AVFilterLink *outlink) |
59 |
|
|
{ |
60 |
|
1 |
AVFilterContext *ctx = outlink->src; |
61 |
|
1 |
ASetRateContext *sr = ctx->priv; |
62 |
|
1 |
AVFilterLink *inlink = ctx->inputs[0]; |
63 |
|
1 |
AVRational intb = ctx->inputs[0]->time_base; |
64 |
|
1 |
int inrate = inlink->sample_rate; |
65 |
|
|
|
66 |
✓✗✓✗
|
1 |
if (intb.num == 1 && intb.den == inrate) { |
67 |
|
1 |
outlink->time_base.num = 1; |
68 |
|
1 |
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 |
|
1 |
return 0; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
20 |
static int filter_frame(AVFilterLink *inlink, AVFrame *frame) |
79 |
|
|
{ |
80 |
|
20 |
AVFilterContext *ctx = inlink->dst; |
81 |
|
20 |
ASetRateContext *sr = ctx->priv; |
82 |
|
20 |
AVFilterLink *outlink = ctx->outputs[0]; |
83 |
|
|
|
84 |
|
20 |
frame->sample_rate = outlink->sample_rate; |
85 |
✗✓ |
20 |
if (sr->rescale_pts) |
86 |
|
|
frame->pts = av_rescale(frame->pts, inlink->sample_rate, |
87 |
|
|
outlink->sample_rate); |
88 |
|
20 |
return ff_filter_frame(outlink, frame); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
static const AVFilterPad asetrate_inputs[] = { |
92 |
|
|
{ |
93 |
|
|
.name = "default", |
94 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
95 |
|
|
.filter_frame = filter_frame, |
96 |
|
|
}, |
97 |
|
|
{ NULL } |
98 |
|
|
}; |
99 |
|
|
|
100 |
|
|
static const AVFilterPad asetrate_outputs[] = { |
101 |
|
|
{ |
102 |
|
|
.name = "default", |
103 |
|
|
.type = AVMEDIA_TYPE_AUDIO, |
104 |
|
|
.config_props = config_props, |
105 |
|
|
}, |
106 |
|
|
{ NULL } |
107 |
|
|
}; |
108 |
|
|
|
109 |
|
|
AVFilter ff_af_asetrate = { |
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 |
|
|
}; |