This filter changes the number of samples on single output operation.
Based on a patch by Andrey Utkin <andrey.krieger.utkin@gmail.com>.
... | ... |
@@ -273,6 +273,36 @@ For example, to resample the input audio to 44100Hz: |
273 | 273 |
aresample=44100 |
274 | 274 |
@end example |
275 | 275 |
|
276 |
+@section asetnsamples |
|
277 |
+ |
|
278 |
+Set the number of samples per each output audio frame. |
|
279 |
+ |
|
280 |
+The last output packet may contain a different number of samples, as |
|
281 |
+the filter will flush all the remaining samples when the input audio |
|
282 |
+signal its end. |
|
283 |
+ |
|
284 |
+The filter accepts parameters as a list of @var{key}=@var{value} pairs, |
|
285 |
+separated by ":". |
|
286 |
+ |
|
287 |
+@table @option |
|
288 |
+ |
|
289 |
+@item nb_out_samples, n |
|
290 |
+Set the number of frames per each output audio frame. The number is |
|
291 |
+intended as the number of samples @emph{per each channel}. |
|
292 |
+Default value is 1024. |
|
293 |
+ |
|
294 |
+@item pad, p |
|
295 |
+If set to 1, the filter will pad the last audio frame with zeroes, so |
|
296 |
+that the last frame will contain the same number of samples as the |
|
297 |
+previous ones. Default value is 1. |
|
298 |
+@end table |
|
299 |
+ |
|
300 |
+For example, to set the number of per-frame samples to 1234 and |
|
301 |
+disable padding for the last frame, use: |
|
302 |
+@example |
|
303 |
+asetnsamples=n=1234:p=0 |
|
304 |
+@end example |
|
305 |
+ |
|
276 | 306 |
@section ashowinfo |
277 | 307 |
|
278 | 308 |
Show a line containing various information for each input audio frame. |
... | ... |
@@ -51,6 +51,7 @@ OBJS-$(CONFIG_AMERGE_FILTER) += af_amerge.o |
51 | 51 |
OBJS-$(CONFIG_AMIX_FILTER) += af_amix.o |
52 | 52 |
OBJS-$(CONFIG_ANULL_FILTER) += af_anull.o |
53 | 53 |
OBJS-$(CONFIG_ARESAMPLE_FILTER) += af_aresample.o |
54 |
+OBJS-$(CONFIG_ASETNSAMPLES_FILTER) += af_asetnsamples.o |
|
54 | 55 |
OBJS-$(CONFIG_ASHOWINFO_FILTER) += af_ashowinfo.o |
55 | 56 |
OBJS-$(CONFIG_ASPLIT_FILTER) += split.o |
56 | 57 |
OBJS-$(CONFIG_ASTREAMSYNC_FILTER) += af_astreamsync.o |
57 | 58 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,206 @@ |
0 |
+/* |
|
1 |
+ * Copyright (c) 2012 Andrey Utkin |
|
2 |
+ * Copyright (c) 2012 Stefano Sabatini |
|
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 |
|
8 |
+ * License 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 GNU |
|
14 |
+ * Lesser General Public License for more details. |
|
15 |
+ * |
|
16 |
+ * You should have received a copy of the GNU Lesser General Public |
|
17 |
+ * License along with FFmpeg; if not, write to the Free Software |
|
18 |
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|
19 |
+ */ |
|
20 |
+ |
|
21 |
+/** |
|
22 |
+ * @file |
|
23 |
+ * Filter that changes number of samples on single output operation |
|
24 |
+ */ |
|
25 |
+ |
|
26 |
+#include "libavutil/audio_fifo.h" |
|
27 |
+#include "libavutil/avassert.h" |
|
28 |
+#include "libavutil/opt.h" |
|
29 |
+#include "avfilter.h" |
|
30 |
+#include "audio.h" |
|
31 |
+#include "formats.h" |
|
32 |
+ |
|
33 |
+typedef struct { |
|
34 |
+ const AVClass *class; |
|
35 |
+ int nb_out_samples; ///< how many samples to output |
|
36 |
+ AVAudioFifo *fifo; ///< samples are queued here |
|
37 |
+ int64_t next_out_pts; |
|
38 |
+ int req_fullfilled; |
|
39 |
+ int pad; |
|
40 |
+} ASNSContext; |
|
41 |
+ |
|
42 |
+#define OFFSET(x) offsetof(ASNSContext, x) |
|
43 |
+ |
|
44 |
+static const AVOption asns_options[] = { |
|
45 |
+{ "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 }, |
|
46 |
+{ "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_INT, {.dbl=1}, 0, 1 }, |
|
47 |
+{ "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX }, |
|
48 |
+{ "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.dbl=1024}, 1, INT_MAX }, |
|
49 |
+{ NULL } |
|
50 |
+}; |
|
51 |
+ |
|
52 |
+static const AVClass asns_class = { |
|
53 |
+ "asetnsamples", |
|
54 |
+ av_default_item_name, |
|
55 |
+ asns_options |
|
56 |
+}; |
|
57 |
+ |
|
58 |
+static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) |
|
59 |
+{ |
|
60 |
+ ASNSContext *asns = ctx->priv; |
|
61 |
+ int err; |
|
62 |
+ |
|
63 |
+ asns->class = &asns_class; |
|
64 |
+ av_opt_set_defaults(asns); |
|
65 |
+ |
|
66 |
+ if ((err = av_set_options_string(asns, args, "=", ":")) < 0) { |
|
67 |
+ av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args); |
|
68 |
+ return err; |
|
69 |
+ } |
|
70 |
+ |
|
71 |
+ asns->next_out_pts = AV_NOPTS_VALUE; |
|
72 |
+ av_log(ctx, AV_LOG_INFO, "nb_out_samples:%d pad:%d\n", asns->nb_out_samples, asns->pad); |
|
73 |
+ |
|
74 |
+ return 0; |
|
75 |
+} |
|
76 |
+ |
|
77 |
+static av_cold void uninit(AVFilterContext *ctx) |
|
78 |
+{ |
|
79 |
+ ASNSContext *asns = ctx->priv; |
|
80 |
+ av_audio_fifo_free(asns->fifo); |
|
81 |
+} |
|
82 |
+ |
|
83 |
+static int config_props_output(AVFilterLink *outlink) |
|
84 |
+{ |
|
85 |
+ ASNSContext *asns = outlink->src->priv; |
|
86 |
+ int nb_channels = av_get_channel_layout_nb_channels(outlink->channel_layout); |
|
87 |
+ |
|
88 |
+ asns->fifo = av_audio_fifo_alloc(outlink->format, nb_channels, asns->nb_out_samples); |
|
89 |
+ if (!asns->fifo) |
|
90 |
+ return AVERROR(ENOMEM); |
|
91 |
+ |
|
92 |
+ return 0; |
|
93 |
+} |
|
94 |
+ |
|
95 |
+static int push_samples(AVFilterLink *outlink) |
|
96 |
+{ |
|
97 |
+ ASNSContext *asns = outlink->src->priv; |
|
98 |
+ AVFilterBufferRef *outsamples = NULL; |
|
99 |
+ int nb_out_samples, nb_pad_samples; |
|
100 |
+ |
|
101 |
+ if (asns->pad) { |
|
102 |
+ nb_out_samples = av_audio_fifo_size(asns->fifo) ? asns->nb_out_samples : 0; |
|
103 |
+ nb_pad_samples = nb_out_samples - FFMIN(nb_out_samples, av_audio_fifo_size(asns->fifo)); |
|
104 |
+ } else { |
|
105 |
+ nb_out_samples = FFMIN(asns->nb_out_samples, av_audio_fifo_size(asns->fifo)); |
|
106 |
+ nb_pad_samples = 0; |
|
107 |
+ } |
|
108 |
+ |
|
109 |
+ if (!nb_out_samples) |
|
110 |
+ return 0; |
|
111 |
+ |
|
112 |
+ outsamples = ff_get_audio_buffer(outlink, AV_PERM_WRITE, nb_out_samples); |
|
113 |
+ av_assert0(outsamples); |
|
114 |
+ |
|
115 |
+ av_audio_fifo_read(asns->fifo, |
|
116 |
+ (void **)outsamples->extended_data, nb_out_samples); |
|
117 |
+ |
|
118 |
+ if (nb_pad_samples) |
|
119 |
+ av_samples_set_silence(outsamples->extended_data, nb_out_samples - nb_pad_samples, |
|
120 |
+ nb_pad_samples, av_get_channel_layout_nb_channels(outlink->channel_layout), |
|
121 |
+ outlink->format); |
|
122 |
+ outsamples->audio->nb_samples = nb_out_samples; |
|
123 |
+ outsamples->audio->channel_layout = outlink->channel_layout; |
|
124 |
+ outsamples->audio->sample_rate = outlink->sample_rate; |
|
125 |
+ outsamples->pts = asns->next_out_pts; |
|
126 |
+ |
|
127 |
+ if (asns->next_out_pts != AV_NOPTS_VALUE) |
|
128 |
+ asns->next_out_pts += nb_out_samples; |
|
129 |
+ |
|
130 |
+ ff_filter_samples(outlink, outsamples); |
|
131 |
+ asns->req_fullfilled = 1; |
|
132 |
+ return nb_out_samples; |
|
133 |
+} |
|
134 |
+ |
|
135 |
+static void filter_samples(AVFilterLink *inlink, AVFilterBufferRef *insamples) |
|
136 |
+{ |
|
137 |
+ AVFilterContext *ctx = inlink->dst; |
|
138 |
+ ASNSContext *asns = ctx->priv; |
|
139 |
+ AVFilterLink *outlink = ctx->outputs[0]; |
|
140 |
+ int ret; |
|
141 |
+ int nb_samples = insamples->audio->nb_samples; |
|
142 |
+ |
|
143 |
+ if (av_audio_fifo_space(asns->fifo) < nb_samples) { |
|
144 |
+ av_log(ctx, AV_LOG_DEBUG, "No space for %d samples, stretching audio fifo\n", nb_samples); |
|
145 |
+ ret = av_audio_fifo_realloc(asns->fifo, av_audio_fifo_size(asns->fifo) + nb_samples); |
|
146 |
+ if (ret < 0) { |
|
147 |
+ av_log(ctx, AV_LOG_ERROR, |
|
148 |
+ "Stretching audio fifo failed, discarded %d samples\n", nb_samples); |
|
149 |
+ return; |
|
150 |
+ } |
|
151 |
+ } |
|
152 |
+ av_audio_fifo_write(asns->fifo, (void **)insamples->extended_data, nb_samples); |
|
153 |
+ if (asns->next_out_pts == AV_NOPTS_VALUE) |
|
154 |
+ asns->next_out_pts = insamples->pts; |
|
155 |
+ avfilter_unref_buffer(insamples); |
|
156 |
+ |
|
157 |
+ if (av_audio_fifo_size(asns->fifo) >= asns->nb_out_samples) |
|
158 |
+ push_samples(outlink); |
|
159 |
+} |
|
160 |
+ |
|
161 |
+static int request_frame(AVFilterLink *outlink) |
|
162 |
+{ |
|
163 |
+ ASNSContext *asns = outlink->src->priv; |
|
164 |
+ AVFilterLink *inlink = outlink->src->inputs[0]; |
|
165 |
+ int ret; |
|
166 |
+ |
|
167 |
+ asns->req_fullfilled = 0; |
|
168 |
+ do { |
|
169 |
+ ret = avfilter_request_frame(inlink); |
|
170 |
+ } while (!asns->req_fullfilled && ret >= 0); |
|
171 |
+ |
|
172 |
+ if (ret == AVERROR_EOF) |
|
173 |
+ while (push_samples(outlink)) |
|
174 |
+ ; |
|
175 |
+ |
|
176 |
+ return ret; |
|
177 |
+} |
|
178 |
+ |
|
179 |
+AVFilter avfilter_af_asetnsamples = { |
|
180 |
+ .name = "asetnsamples", |
|
181 |
+ .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."), |
|
182 |
+ .priv_size = sizeof(ASNSContext), |
|
183 |
+ .init = init, |
|
184 |
+ .uninit = uninit, |
|
185 |
+ |
|
186 |
+ .inputs = (const AVFilterPad[]) { |
|
187 |
+ { |
|
188 |
+ .name = "default", |
|
189 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
190 |
+ .filter_samples = filter_samples, |
|
191 |
+ .min_perms = AV_PERM_READ|AV_PERM_WRITE |
|
192 |
+ }, |
|
193 |
+ { .name = NULL } |
|
194 |
+ }, |
|
195 |
+ |
|
196 |
+ .outputs = (const AVFilterPad[]) { |
|
197 |
+ { |
|
198 |
+ .name = "default", |
|
199 |
+ .type = AVMEDIA_TYPE_AUDIO, |
|
200 |
+ .request_frame = request_frame, |
|
201 |
+ .config_props = config_props_output, |
|
202 |
+ }, |
|
203 |
+ { .name = NULL } |
|
204 |
+ }, |
|
205 |
+}; |
... | ... |
@@ -40,6 +40,7 @@ void avfilter_register_all(void) |
40 | 40 |
REGISTER_FILTER (AMIX, amix, af); |
41 | 41 |
REGISTER_FILTER (ANULL, anull, af); |
42 | 42 |
REGISTER_FILTER (ARESAMPLE, aresample, af); |
43 |
+ REGISTER_FILTER (ASETNSAMPLES, asetnsamples, af); |
|
43 | 44 |
REGISTER_FILTER (ASHOWINFO, ashowinfo, af); |
44 | 45 |
REGISTER_FILTER (ASPLIT, asplit, af); |
45 | 46 |
REGISTER_FILTER (ASTREAMSYNC, astreamsync, af); |
... | ... |
@@ -29,7 +29,7 @@ |
29 | 29 |
#include "libavutil/avutil.h" |
30 | 30 |
|
31 | 31 |
#define LIBAVFILTER_VERSION_MAJOR 2 |
32 |
-#define LIBAVFILTER_VERSION_MINOR 79 |
|
32 |
+#define LIBAVFILTER_VERSION_MINOR 80 |
|
33 | 33 |
#define LIBAVFILTER_VERSION_MICRO 100 |
34 | 34 |
|
35 | 35 |
#define LIBAVFILTER_VERSION_INT AV_VERSION_INT(LIBAVFILTER_VERSION_MAJOR, \ |