Browse code

lavfi: add asetrate filter.

Nicolas George authored on 2013/04/18 23:59:27
Showing 6 changed files
... ...
@@ -28,6 +28,7 @@ version <next>:
28 28
 - The matroska demuxer can now output proper verbatim ASS packets. It will
29 29
   become the default at the next libavformat major bump.
30 30
 - decent native animated GIF encoding
31
+- asetrate filter
31 32
 
32 33
 
33 34
 version 1.2:
... ...
@@ -900,6 +900,18 @@ disable padding for the last frame, use:
900 900
 asetnsamples=n=1234:p=0
901 901
 @end example
902 902
 
903
+@section asetrate
904
+
905
+Set the sample rate without altering the PCM data.
906
+This will result in a change of speed and pitch.
907
+
908
+The filter accepts the following options:
909
+
910
+@table @option
911
+@item sample_rate, r
912
+Set the output sample rate. Default is 44100 Hz.
913
+@end table
914
+
903 915
 @section ashowinfo
904 916
 
905 917
 Show a line containing various information for each input audio frame.
... ...
@@ -64,6 +64,7 @@ OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
64 64
 OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
65 65
 OBJS-$(CONFIG_ASETNSAMPLES_FILTER)           += af_asetnsamples.o
66 66
 OBJS-$(CONFIG_ASETPTS_FILTER)                += f_setpts.o
67
+OBJS-$(CONFIG_ASETRATE_FILTER)               += af_asetrate.o
67 68
 OBJS-$(CONFIG_ASETTB_FILTER)                 += f_settb.o
68 69
 OBJS-$(CONFIG_ASHOWINFO_FILTER)              += af_ashowinfo.o
69 70
 OBJS-$(CONFIG_ASPLIT_FILTER)                 += split.o
70 71
new file mode 100644
... ...
@@ -0,0 +1,119 @@
0
+/*
1
+ * Copyright (c) 2013 Nicolas George
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public License
7
+ * as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public License
16
+ * along with FFmpeg; if not, write to the Free Software Foundation, Inc.,
17
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+#include "libavutil/opt.h"
21
+#include "avfilter.h"
22
+#include "internal.h"
23
+
24
+typedef struct {
25
+    const AVClass *class;
26
+    int sample_rate;
27
+    int rescale_pts;
28
+} ASetRateContext;
29
+
30
+#define CONTEXT ASetRateContext
31
+#define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
32
+
33
+#define OPT_GENERIC(name, field, def, min, max, descr, type, deffield, ...) \
34
+    { name, descr, offsetof(CONTEXT, field), AV_OPT_TYPE_ ## type,          \
35
+      { .deffield = def }, min, max, FLAGS, __VA_ARGS__ }
36
+
37
+#define OPT_INT(name, field, def, min, max, descr, ...) \
38
+    OPT_GENERIC(name, field, def, min, max, descr, INT, i64, __VA_ARGS__)
39
+
40
+static const AVOption asetrate_options[] = {
41
+    OPT_INT("sample_rate", sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
42
+    OPT_INT("r",           sample_rate, 44100, 1, INT_MAX, "set the sample rate"),
43
+    {NULL},
44
+};
45
+
46
+AVFILTER_DEFINE_CLASS(asetrate);
47
+
48
+static av_cold int query_formats(AVFilterContext *ctx)
49
+{
50
+    ASetRateContext *sr = ctx->priv;
51
+    int sample_rates[] = { sr->sample_rate, -1 };
52
+
53
+    ff_formats_ref(ff_make_format_list(sample_rates),
54
+                   &ctx->outputs[0]->in_samplerates);
55
+    return 0;
56
+}
57
+
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;
65
+
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
+}
77
+
78
+static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
79
+{
80
+    AVFilterContext *ctx = inlink->dst;
81
+    ASetRateContext *sr = ctx->priv;
82
+    AVFilterLink *outlink = ctx->outputs[0];
83
+
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
+}
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 avfilter_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
+};
... ...
@@ -62,6 +62,7 @@ void avfilter_register_all(void)
62 62
     REGISTER_FILTER(ASENDCMD,       asendcmd,       af);
63 63
     REGISTER_FILTER(ASETNSAMPLES,   asetnsamples,   af);
64 64
     REGISTER_FILTER(ASETPTS,        asetpts,        af);
65
+    REGISTER_FILTER(ASETRATE,       asetrate,       af);
65 66
     REGISTER_FILTER(ASETTB,         asettb,         af);
66 67
     REGISTER_FILTER(ASHOWINFO,      ashowinfo,      af);
67 68
     REGISTER_FILTER(ASPLIT,         asplit,         af);
... ...
@@ -29,7 +29,7 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  58
32
+#define LIBAVFILTER_VERSION_MINOR  59
33 33
 #define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \