Line | Branch | Exec | Source |
---|---|---|---|
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 "filters.h" | ||
24 | #include "formats.h" | ||
25 | |||
26 | typedef struct ASetRateContext { | ||
27 | const AVClass *class; | ||
28 | int sample_rate; | ||
29 | int rescale_pts; | ||
30 | } ASetRateContext; | ||
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 | |||
50 | 1 | static av_cold int query_formats(const AVFilterContext *ctx, | |
51 | AVFilterFormatsConfig **cfg_in, | ||
52 | AVFilterFormatsConfig **cfg_out) | ||
53 | { | ||
54 | 1 | const ASetRateContext *sr = ctx->priv; | |
55 | 1 | int ret, sample_rates[] = { sr->sample_rate, -1 }; | |
56 | |||
57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if ((ret = ff_formats_ref(ff_all_samplerates(), |
58 | 1 | &cfg_in[0]->samplerates)) < 0) | |
59 | ✗ | return ret; | |
60 | |||
61 | 1 | return ff_formats_ref(ff_make_format_list(sample_rates), | |
62 | 1 | &cfg_out[0]->samplerates); | |
63 | } | ||
64 | |||
65 | 1 | static av_cold int config_props(AVFilterLink *outlink) | |
66 | { | ||
67 | 1 | AVFilterContext *ctx = outlink->src; | |
68 | 1 | ASetRateContext *sr = ctx->priv; | |
69 | 1 | AVFilterLink *inlink = ctx->inputs[0]; | |
70 | 1 | AVRational intb = ctx->inputs[0]->time_base; | |
71 | 1 | int inrate = inlink->sample_rate; | |
72 | |||
73 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | if (intb.num == 1 && intb.den == inrate) { |
74 | 1 | outlink->time_base.num = 1; | |
75 | 1 | outlink->time_base.den = outlink->sample_rate; | |
76 | } else { | ||
77 | ✗ | outlink->time_base = intb; | |
78 | ✗ | sr->rescale_pts = 1; | |
79 | ✗ | if (av_q2d(intb) > 1.0 / FFMAX(inrate, outlink->sample_rate)) | |
80 | ✗ | av_log(ctx, AV_LOG_WARNING, "Time base is inaccurate\n"); | |
81 | } | ||
82 | 1 | return 0; | |
83 | } | ||
84 | |||
85 | 20 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
86 | { | ||
87 | 20 | AVFilterContext *ctx = inlink->dst; | |
88 | 20 | ASetRateContext *sr = ctx->priv; | |
89 | 20 | AVFilterLink *outlink = ctx->outputs[0]; | |
90 | |||
91 | 20 | frame->sample_rate = outlink->sample_rate; | |
92 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
|
20 | if (sr->rescale_pts) |
93 | ✗ | frame->pts = av_rescale(frame->pts, inlink->sample_rate, | |
94 | ✗ | outlink->sample_rate); | |
95 | 20 | return ff_filter_frame(outlink, frame); | |
96 | } | ||
97 | |||
98 | static const AVFilterPad asetrate_inputs[] = { | ||
99 | { | ||
100 | .name = "default", | ||
101 | .type = AVMEDIA_TYPE_AUDIO, | ||
102 | .filter_frame = filter_frame, | ||
103 | }, | ||
104 | }; | ||
105 | |||
106 | static const AVFilterPad asetrate_outputs[] = { | ||
107 | { | ||
108 | .name = "default", | ||
109 | .type = AVMEDIA_TYPE_AUDIO, | ||
110 | .config_props = config_props, | ||
111 | }, | ||
112 | }; | ||
113 | |||
114 | const AVFilter ff_af_asetrate = { | ||
115 | .name = "asetrate", | ||
116 | .description = NULL_IF_CONFIG_SMALL("Change the sample rate without " | ||
117 | "altering the data."), | ||
118 | .priv_size = sizeof(ASetRateContext), | ||
119 | FILTER_INPUTS(asetrate_inputs), | ||
120 | FILTER_OUTPUTS(asetrate_outputs), | ||
121 | FILTER_QUERY_FUNC2(query_formats), | ||
122 | .priv_class = &asetrate_class, | ||
123 | .flags = AVFILTER_FLAG_METADATA_ONLY, | ||
124 | }; | ||
125 |