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