Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | * Copyright (c) 2012 Michael Niedermayer | ||
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 | * 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 "filters.h" | ||
36 | #include "internal.h" | ||
37 | |||
38 | typedef struct APadContext { | ||
39 | const AVClass *class; | ||
40 | int64_t next_pts; | ||
41 | |||
42 | int eof; | ||
43 | int packet_size; | ||
44 | int64_t pad_len, pad_len_left; | ||
45 | int64_t whole_len, whole_len_left; | ||
46 | int64_t pad_dur; | ||
47 | int64_t whole_dur; | ||
48 | } APadContext; | ||
49 | |||
50 | #define OFFSET(x) offsetof(APadContext, x) | ||
51 | #define A AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM | ||
52 | |||
53 | static const AVOption apad_options[] = { | ||
54 | { "packet_size", "set silence packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, { .i64 = 4096 }, 0, INT_MAX, A }, | ||
55 | { "pad_len", "set number of samples of silence to add", OFFSET(pad_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A }, | ||
56 | { "whole_len", "set minimum target number of samples in the audio stream", OFFSET(whole_len), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, A }, | ||
57 | { "pad_dur", "set duration of silence to add", OFFSET(pad_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A }, | ||
58 | { "whole_dur", "set minimum target duration in the audio stream", OFFSET(whole_dur), AV_OPT_TYPE_DURATION, { .i64 = -1 }, -1, INT64_MAX, A }, | ||
59 | { NULL } | ||
60 | }; | ||
61 | |||
62 | AVFILTER_DEFINE_CLASS(apad); | ||
63 | |||
64 | 2 | static av_cold int init(AVFilterContext *ctx) | |
65 | { | ||
66 | 2 | APadContext *s = ctx->priv; | |
67 | |||
68 | 2 | s->next_pts = AV_NOPTS_VALUE; | |
69 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (s->whole_len >= 0 && s->pad_len >= 0) { |
70 | ✗ | av_log(ctx, AV_LOG_ERROR, "Both whole and pad length are set, this is not possible\n"); | |
71 | ✗ | return AVERROR(EINVAL); | |
72 | } | ||
73 | |||
74 | 2 | return 0; | |
75 | } | ||
76 | |||
77 | 65 | static int filter_frame(AVFilterLink *inlink, AVFrame *frame) | |
78 | { | ||
79 | 65 | AVFilterContext *ctx = inlink->dst; | |
80 | 65 | APadContext *s = ctx->priv; | |
81 | |||
82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (s->whole_len >= 0) { |
83 | ✗ | s->whole_len_left = FFMAX(s->whole_len_left - frame->nb_samples, 0); | |
84 | ✗ | av_log(ctx, AV_LOG_DEBUG, | |
85 | "n_out:%d whole_len_left:%"PRId64"\n", frame->nb_samples, s->whole_len_left); | ||
86 | } | ||
87 | |||
88 | 65 | s->next_pts = frame->pts + av_rescale_q(frame->nb_samples, (AVRational){1, inlink->sample_rate}, inlink->time_base); | |
89 | 65 | return ff_filter_frame(ctx->outputs[0], frame); | |
90 | } | ||
91 | |||
92 | 2 | static int push_frame(AVFilterLink *outlink) | |
93 | { | ||
94 | 2 | AVFilterContext *ctx = outlink->src; | |
95 | 2 | APadContext *s = ctx->priv; | |
96 | AVFrame *outsamplesref; | ||
97 | int n_out; | ||
98 | |||
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (ctx->is_disabled) |
100 | ✗ | return 0; | |
101 | 2 | n_out = s->packet_size; | |
102 | |||
103 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (s->whole_len >= 0 && s->pad_len < 0) { |
104 | ✗ | s->pad_len = s->pad_len_left = s->whole_len_left; | |
105 | } | ||
106 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
2 | if (s->pad_len >=0 || s->whole_len >= 0) { |
107 | 2 | n_out = FFMIN(n_out, s->pad_len_left); | |
108 | 2 | s->pad_len_left -= n_out; | |
109 | 2 | av_log(ctx, AV_LOG_DEBUG, | |
110 | "padding n_out:%d pad_len_left:%"PRId64"\n", n_out, s->pad_len_left); | ||
111 | } | ||
112 | |||
113 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (!n_out) |
114 | 1 | return AVERROR_EOF; | |
115 | |||
116 | 1 | outsamplesref = ff_get_audio_buffer(outlink, n_out); | |
117 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!outsamplesref) |
118 | ✗ | return AVERROR(ENOMEM); | |
119 | |||
120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(outsamplesref->sample_rate == outlink->sample_rate); |
121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | av_assert0(outsamplesref->nb_samples == n_out); |
122 | |||
123 | 1 | av_samples_set_silence(outsamplesref->extended_data, 0, | |
124 | n_out, | ||
125 | outsamplesref->ch_layout.nb_channels, | ||
126 | 1 | outsamplesref->format); | |
127 | |||
128 | 1 | outsamplesref->pts = s->next_pts; | |
129 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (s->next_pts != AV_NOPTS_VALUE) |
130 | 1 | s->next_pts += av_rescale_q(n_out, (AVRational){1, outlink->sample_rate}, outlink->time_base); | |
131 | |||
132 | 1 | return ff_filter_frame(outlink, outsamplesref); | |
133 | } | ||
134 | |||
135 | 132 | static int activate(AVFilterContext *ctx) | |
136 | { | ||
137 | 132 | AVFilterLink *inlink = ctx->inputs[0]; | |
138 | 132 | AVFilterLink *outlink = ctx->outputs[0]; | |
139 | 132 | APadContext *s = ctx->priv; | |
140 | int64_t pts; | ||
141 | int status; | ||
142 | |||
143 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 132 times.
|
132 | FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink); |
144 | |||
145 |
4/4✓ Branch 0 taken 131 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 65 times.
✓ Branch 4 taken 66 times.
|
132 | if (!s->eof && ff_inlink_queued_frames(inlink)) { |
146 | 65 | AVFrame *frame = NULL; | |
147 | int ret; | ||
148 | |||
149 | 65 | ret = ff_inlink_consume_frame(inlink, &frame); | |
150 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 65 times.
|
65 | if (ret < 0) |
151 | 65 | return ret; | |
152 |
1/2✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
|
65 | if (ret > 0) |
153 | 65 | return filter_frame(inlink, frame); | |
154 | } | ||
155 | |||
156 |
4/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 65 times.
|
67 | if (!s->eof && ff_inlink_acknowledge_status(inlink, &status, &pts)) |
157 | 1 | s->eof = status == AVERROR_EOF; | |
158 | |||
159 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 65 times.
|
67 | if (s->eof) { |
160 | 2 | int ret = push_frame(outlink); | |
161 | |||
162 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (ret == AVERROR_EOF) { |
163 | 1 | ff_outlink_set_status(outlink, AVERROR_EOF, s->next_pts); | |
164 | 1 | return 0; | |
165 | } | ||
166 | 1 | return ret; | |
167 | } | ||
168 | |||
169 |
1/2✓ Branch 1 taken 65 times.
✗ Branch 2 not taken.
|
65 | FF_FILTER_FORWARD_WANTED(outlink, inlink); |
170 | |||
171 | ✗ | return FFERROR_NOT_READY; | |
172 | } | ||
173 | |||
174 | 1 | static int config_output(AVFilterLink *outlink) | |
175 | { | ||
176 | 1 | AVFilterContext *ctx = outlink->src; | |
177 | 1 | APadContext *s = ctx->priv; | |
178 | |||
179 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->pad_dur >= 0) |
180 | ✗ | s->pad_len = av_rescale(s->pad_dur, outlink->sample_rate, AV_TIME_BASE); | |
181 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (s->whole_dur >= 0) |
182 | ✗ | s->whole_len = av_rescale(s->whole_dur, outlink->sample_rate, AV_TIME_BASE); | |
183 | |||
184 | 1 | s->pad_len_left = s->pad_len; | |
185 | 1 | s->whole_len_left = s->whole_len; | |
186 | |||
187 | 1 | return 0; | |
188 | } | ||
189 | |||
190 | static const AVFilterPad apad_outputs[] = { | ||
191 | { | ||
192 | .name = "default", | ||
193 | .type = AVMEDIA_TYPE_AUDIO, | ||
194 | .config_props = config_output, | ||
195 | }, | ||
196 | }; | ||
197 | |||
198 | const AVFilter ff_af_apad = { | ||
199 | .name = "apad", | ||
200 | .description = NULL_IF_CONFIG_SMALL("Pad audio with silence."), | ||
201 | .init = init, | ||
202 | .activate = activate, | ||
203 | .priv_size = sizeof(APadContext), | ||
204 | FILTER_INPUTS(ff_audio_default_filterpad), | ||
205 | FILTER_OUTPUTS(apad_outputs), | ||
206 | .priv_class = &apad_class, | ||
207 | .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL, | ||
208 | }; | ||
209 |