Browse code

avfilter: add apad filter

This filter pads an audio stream with silence
It can together with -shortest be used to extend audio streams to
the same length as video.

Signed-off-by: Michael Niedermayer <michaelni@gmx.at>

Michael Niedermayer authored on 2012/12/18 13:13:59
Showing 6 changed files
... ...
@@ -45,6 +45,7 @@ version <next>:
45 45
 - SGI RLE 8-bit decoder
46 46
 - Silicon Graphics Motion Video Compressor 1 & 2 decoder
47 47
 - Silicon Graphics Movie demuxer
48
+- apad filter
48 49
 
49 50
 
50 51
 version 1.0:
... ...
@@ -409,6 +409,11 @@ stream ends. The default value is 2 seconds.
409 409
 
410 410
 Pass the audio source unchanged to the output.
411 411
 
412
+@section apad
413
+
414
+Pad the end of a audio stream with silence, this can be used together with
415
+-shortest to extend audio streams to the same length as the video stream.
416
+
412 417
 @section aresample
413 418
 
414 419
 Resample the input audio to the specified parameters. If none are specified
... ...
@@ -53,6 +53,7 @@ OBJS-$(CONFIG_AFORMAT_FILTER)                += af_aformat.o
53 53
 OBJS-$(CONFIG_AMERGE_FILTER)                 += af_amerge.o
54 54
 OBJS-$(CONFIG_AMIX_FILTER)                   += af_amix.o
55 55
 OBJS-$(CONFIG_ANULL_FILTER)                  += af_anull.o
56
+OBJS-$(CONFIG_APAD_FILTER)                   += af_apad.o
56 57
 OBJS-$(CONFIG_ARESAMPLE_FILTER)              += af_aresample.o
57 58
 OBJS-$(CONFIG_ASELECT_FILTER)                += f_select.o
58 59
 OBJS-$(CONFIG_ASENDCMD_FILTER)               += f_sendcmd.o
59 60
new file mode 100644
... ...
@@ -0,0 +1,116 @@
0
+/*
1
+ * Copyright (c) 2012 Michael Niedermayer
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
7
+ * License 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 GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ *
19
+ */
20
+
21
+/**
22
+ * @file
23
+ * audio pad filter.
24
+ *
25
+ * Based on af_aresample.c
26
+ */
27
+
28
+#include "libavutil/avstring.h"
29
+#include "libavutil/channel_layout.h"
30
+#include "libavutil/opt.h"
31
+#include "libavutil/samplefmt.h"
32
+#include "libavutil/avassert.h"
33
+#include "avfilter.h"
34
+#include "audio.h"
35
+#include "internal.h"
36
+
37
+typedef struct {
38
+    int64_t next_pts;
39
+} APadContext;
40
+
41
+static av_cold int init(AVFilterContext *ctx, const char *args)
42
+{
43
+    APadContext *apad = ctx->priv;
44
+
45
+    apad->next_pts = AV_NOPTS_VALUE;
46
+
47
+    return 0;
48
+}
49
+
50
+static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
51
+{
52
+    AVFilterContext *ctx = inlink->dst;
53
+    APadContext *apad = ctx->priv;
54
+    apad->next_pts = frame->pts + av_rescale_q(frame->audio->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base);
55
+    return ff_filter_frame(ctx->outputs[0], frame);
56
+}
57
+
58
+static int request_frame(AVFilterLink *outlink)
59
+{
60
+    AVFilterContext *ctx = outlink->src;
61
+    APadContext *apad = ctx->priv;
62
+    int ret;
63
+
64
+    ret = ff_request_frame(ctx->inputs[0]);
65
+
66
+    if (ret == AVERROR_EOF) {
67
+        int n_out = 4096;
68
+        AVFilterBufferRef *outsamplesref = ff_get_audio_buffer(outlink, AV_PERM_WRITE, n_out);
69
+        if (!outsamplesref)
70
+            return AVERROR(ENOMEM);
71
+
72
+        av_assert0(outsamplesref->audio->sample_rate == outlink->sample_rate);
73
+        av_assert0(outsamplesref->audio->nb_samples  == n_out);
74
+
75
+        av_samples_set_silence(outsamplesref->extended_data, 0,
76
+                               n_out,
77
+                               outsamplesref->audio->channels,
78
+                               outsamplesref->format);
79
+
80
+        outsamplesref->pts = apad->next_pts;
81
+        if (apad->next_pts != AV_NOPTS_VALUE)
82
+            apad->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base);
83
+
84
+        return ff_filter_frame(outlink, outsamplesref);
85
+    }
86
+    return ret;
87
+}
88
+
89
+static const AVFilterPad apad_inputs[] = {
90
+    {
91
+        .name         = "default",
92
+        .type         = AVMEDIA_TYPE_AUDIO,
93
+        .filter_frame = filter_frame,
94
+        .min_perms    = AV_PERM_READ,
95
+    },
96
+    { NULL },
97
+};
98
+
99
+static const AVFilterPad apad_outputs[] = {
100
+    {
101
+        .name          = "default",
102
+        .request_frame = request_frame,
103
+        .type          = AVMEDIA_TYPE_AUDIO,
104
+    },
105
+    { NULL },
106
+};
107
+
108
+AVFilter avfilter_af_apad = {
109
+    .name          = "apad",
110
+    .description   = NULL_IF_CONFIG_SMALL("Pad audio with silence."),
111
+    .init          = init,
112
+    .priv_size     = sizeof(APadContext),
113
+    .inputs        = apad_inputs,
114
+    .outputs       = apad_outputs,
115
+};
... ...
@@ -45,6 +45,7 @@ void avfilter_register_all(void)
45 45
     REGISTER_FILTER (AMERGE,      amerge,      af);
46 46
     REGISTER_FILTER (AMIX,        amix,        af);
47 47
     REGISTER_FILTER (ANULL,       anull,       af);
48
+    REGISTER_FILTER (APAD,        apad,        af);
48 49
     REGISTER_FILTER (ARESAMPLE,   aresample,   af);
49 50
     REGISTER_FILTER (ASELECT,     aselect,     af);
50 51
     REGISTER_FILTER (ASENDCMD,    asendcmd,    af);
... ...
@@ -29,8 +29,8 @@
29 29
 #include "libavutil/avutil.h"
30 30
 
31 31
 #define LIBAVFILTER_VERSION_MAJOR  3
32
-#define LIBAVFILTER_VERSION_MINOR  27
33
-#define LIBAVFILTER_VERSION_MICRO 102
32
+#define LIBAVFILTER_VERSION_MINOR  28
33
+#define LIBAVFILTER_VERSION_MICRO 100
34 34
 
35 35
 #define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \
36 36
                                                LIBAVFILTER_VERSION_MINOR, \