1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
* This file is part of Libav.
*
* Libav is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Libav is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Libav; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <float.h>
#include <math.h>
#include "config.h"
#include "libavutil/avassert.h"
#include "libavutil/channel_layout.h"
#include "libavutil/common.h"
#include "libavutil/log.h"
#include "libavutil/mathematics.h"
#include "libavutil/opt.h"
#include "libavutil/samplefmt.h"
#include "audio.h"
#include "avfilter.h"
#include "internal.h"
typedef struct TrimContext {
const AVClass *class;
/*
* AVOptions
*/
double duration;
double start_time, end_time;
int64_t start_frame, end_frame;
/*
* in the link timebase for video,
* in 1/samplerate for audio
*/
int64_t start_pts, end_pts;
int64_t start_sample, end_sample;
/*
* number of video frames that arrived on this filter so far
*/
int64_t nb_frames;
/*
* number of audio samples that arrived on this filter so far
*/
int64_t nb_samples;
/*
* timestamp of the first frame in the output, in the timebase units
*/
int64_t first_pts;
/*
* duration in the timebase units
*/
int64_t duration_tb;
int64_t next_pts;
int eof;
int got_output;
} TrimContext;
static int init(AVFilterContext *ctx)
{
TrimContext *s = ctx->priv;
s->first_pts = AV_NOPTS_VALUE;
return 0;
}
static int config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
TrimContext *s = ctx->priv;
AVRational tb = (inlink->type == AVMEDIA_TYPE_VIDEO) ?
inlink->time_base : (AVRational){ 1, inlink->sample_rate };
if (s->start_time != DBL_MAX) {
int64_t start_pts = lrintf(s->start_time / av_q2d(tb));
if (s->start_pts == AV_NOPTS_VALUE || start_pts < s->start_pts)
s->start_pts = start_pts;
}
if (s->end_time != DBL_MAX) {
int64_t end_pts = lrintf(s->end_time / av_q2d(tb));
if (s->end_pts == AV_NOPTS_VALUE || end_pts > s->end_pts)
s->end_pts = end_pts;
}
if (s->duration)
s->duration_tb = lrintf(s->duration / av_q2d(tb));
return 0;
}
static int request_frame(AVFilterLink *outlink)
{
AVFilterContext *ctx = outlink->src;
TrimContext *s = ctx->priv;
int ret;
s->got_output = 0;
while (!s->got_output) {
if (s->eof)
return AVERROR_EOF;
ret = ff_request_frame(ctx->inputs[0]);
if (ret < 0)
return ret;
}
return 0;
}
#define OFFSET(x) offsetof(TrimContext, x)
#define COMMON_OPTS \
{ "start", "Timestamp in seconds of the first frame that " \
"should be passed", OFFSET(start_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
{ "end", "Timestamp in seconds of the first frame that " \
"should be dropped again", OFFSET(end_time), AV_OPT_TYPE_DOUBLE, { .dbl = DBL_MAX }, -DBL_MAX, DBL_MAX, FLAGS }, \
{ "start_pts", "Timestamp of the first frame that should be " \
" passed", OFFSET(start_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
{ "end_pts", "Timestamp of the first frame that should be " \
"dropped again", OFFSET(end_pts), AV_OPT_TYPE_INT64, { .i64 = AV_NOPTS_VALUE }, INT64_MIN, INT64_MAX, FLAGS }, \
{ "duration", "Maximum duration of the output in seconds", OFFSET(duration), AV_OPT_TYPE_DOUBLE, { .dbl = 0 }, 0, DBL_MAX, FLAGS },
#if CONFIG_TRIM_FILTER
static int trim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
AVFilterContext *ctx = inlink->dst;
TrimContext *s = ctx->priv;
int drop;
/* drop everything if EOF has already been returned */
if (s->eof) {
av_frame_free(&frame);
return 0;
}
if (s->start_frame >= 0 || s->start_pts != AV_NOPTS_VALUE) {
drop = 1;
if (s->start_frame >= 0 && s->nb_frames >= s->start_frame)
drop = 0;
if (s->start_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
frame->pts >= s->start_pts)
drop = 0;
if (drop)
goto drop;
}
if (s->first_pts == AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE)
s->first_pts = frame->pts;
if (s->end_frame != INT64_MAX || s->end_pts != AV_NOPTS_VALUE || s->duration_tb) {
drop = 1;
if (s->end_frame != INT64_MAX && s->nb_frames < s->end_frame)
drop = 0;
if (s->end_pts != AV_NOPTS_VALUE && frame->pts != AV_NOPTS_VALUE &&
frame->pts < s->end_pts)
drop = 0;
if (s->duration_tb && frame->pts != AV_NOPTS_VALUE &&
frame->pts - s->first_pts < s->duration_tb)
drop = 0;
if (drop) {
s->eof = 1;
goto drop;
}
}
s->nb_frames++;
s->got_output = 1;
return ff_filter_frame(ctx->outputs[0], frame);
drop:
s->nb_frames++;
av_frame_free(&frame);
return 0;
}
#define FLAGS AV_OPT_FLAG_VIDEO_PARAM
static const AVOption trim_options[] = {
COMMON_OPTS
{ "start_frame", "Number of the first frame that should be passed "
"to the output", OFFSET(start_frame), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
{ "end_frame", "Number of the first frame that should be dropped "
"again", OFFSET(end_frame), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
{ NULL },
};
#undef FLAGS
static const AVClass trim_class = {
.class_name = "trim",
.item_name = av_default_item_name,
.option = trim_options,
.version = LIBAVUTIL_VERSION_INT,
};
static const AVFilterPad trim_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.filter_frame = trim_filter_frame,
.config_props = config_input,
},
{ NULL }
};
static const AVFilterPad trim_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_VIDEO,
.request_frame = request_frame,
},
{ NULL }
};
AVFilter avfilter_vf_trim = {
.name = "trim",
.description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
.init = init,
.priv_size = sizeof(TrimContext),
.priv_class = &trim_class,
.inputs = trim_inputs,
.outputs = trim_outputs,
};
#endif // CONFIG_TRIM_FILTER
#if CONFIG_ATRIM_FILTER
static int atrim_filter_frame(AVFilterLink *inlink, AVFrame *frame)
{
AVFilterContext *ctx = inlink->dst;
TrimContext *s = ctx->priv;
int64_t start_sample, end_sample = frame->nb_samples;
int64_t pts;
int drop;
/* drop everything if EOF has already been returned */
if (s->eof) {
av_frame_free(&frame);
return 0;
}
if (frame->pts != AV_NOPTS_VALUE)
pts = av_rescale_q(frame->pts, inlink->time_base,
(AVRational){ 1, inlink->sample_rate });
else
pts = s->next_pts;
s->next_pts = pts + frame->nb_samples;
/* check if at least a part of the frame is after the start time */
if (s->start_sample < 0 && s->start_pts == AV_NOPTS_VALUE) {
start_sample = 0;
} else {
drop = 1;
start_sample = frame->nb_samples;
if (s->start_sample >= 0 &&
s->nb_samples + frame->nb_samples > s->start_sample) {
drop = 0;
start_sample = FFMIN(start_sample, s->start_sample - s->nb_samples);
}
if (s->start_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
pts + frame->nb_samples > s->start_pts) {
drop = 0;
start_sample = FFMIN(start_sample, s->start_pts - pts);
}
if (drop)
goto drop;
}
if (s->first_pts == AV_NOPTS_VALUE)
s->first_pts = pts + start_sample;
/* check if at least a part of the frame is before the end time */
if (s->end_sample == INT64_MAX && s->end_pts == AV_NOPTS_VALUE && !s->duration_tb) {
end_sample = frame->nb_samples;
} else {
drop = 1;
end_sample = 0;
if (s->end_sample != INT64_MAX &&
s->nb_samples < s->end_sample) {
drop = 0;
end_sample = FFMAX(end_sample, s->end_sample - s->nb_samples);
}
if (s->end_pts != AV_NOPTS_VALUE && pts != AV_NOPTS_VALUE &&
pts < s->end_pts) {
drop = 0;
end_sample = FFMAX(end_sample, s->end_pts - pts);
}
if (s->duration_tb && pts - s->first_pts < s->duration_tb) {
drop = 0;
end_sample = FFMAX(end_sample, s->first_pts + s->duration_tb - pts);
}
if (drop) {
s->eof = 1;
goto drop;
}
}
s->nb_samples += frame->nb_samples;
start_sample = FFMAX(0, start_sample);
end_sample = FFMIN(frame->nb_samples, end_sample);
av_assert0(start_sample < end_sample);
if (start_sample) {
AVFrame *out = ff_get_audio_buffer(ctx->outputs[0], end_sample - start_sample);
if (!out) {
av_frame_free(&frame);
return AVERROR(ENOMEM);
}
av_frame_copy_props(out, frame);
av_samples_copy(out->extended_data, frame->extended_data, 0, start_sample,
out->nb_samples, av_get_channel_layout_nb_channels(frame->channel_layout),
frame->format);
if (out->pts != AV_NOPTS_VALUE)
out->pts += av_rescale_q(start_sample, (AVRational){ 1, out->sample_rate },
inlink->time_base);
av_frame_free(&frame);
frame = out;
} else
frame->nb_samples = end_sample;
s->got_output = 1;
return ff_filter_frame(ctx->outputs[0], frame);
drop:
s->nb_samples += frame->nb_samples;
av_frame_free(&frame);
return 0;
}
#define FLAGS AV_OPT_FLAG_AUDIO_PARAM
static const AVOption atrim_options[] = {
COMMON_OPTS
{ "start_sample", "Number of the first audio sample that should be "
"passed to the output", OFFSET(start_sample), AV_OPT_TYPE_INT64, { .i64 = -1 }, -1, INT64_MAX, FLAGS },
{ "end_sample", "Number of the first audio sample that should be "
"dropped again", OFFSET(end_sample), AV_OPT_TYPE_INT64, { .i64 = INT64_MAX }, 0, INT64_MAX, FLAGS },
{ NULL },
};
#undef FLAGS
static const AVClass atrim_class = {
.class_name = "atrim",
.item_name = av_default_item_name,
.option = atrim_options,
.version = LIBAVUTIL_VERSION_INT,
};
static const AVFilterPad atrim_inputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.filter_frame = atrim_filter_frame,
.config_props = config_input,
},
{ NULL }
};
static const AVFilterPad atrim_outputs[] = {
{
.name = "default",
.type = AVMEDIA_TYPE_AUDIO,
.request_frame = request_frame,
},
{ NULL }
};
AVFilter avfilter_af_atrim = {
.name = "atrim",
.description = NULL_IF_CONFIG_SMALL("Pick one continuous section from the input, drop the rest."),
.init = init,
.priv_size = sizeof(TrimContext),
.priv_class = &atrim_class,
.inputs = atrim_inputs,
.outputs = atrim_outputs,
};
#endif // CONFIG_ATRIM_FILTER
|