FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_asetnsamples.c
Date: 2024-03-28 14:59:00
Exec Total Coverage
Lines: 27 36 75.0%
Functions: 1 1 100.0%
Branches: 17 24 70.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2012 Andrey Utkin
3 * Copyright (c) 2012 Stefano Sabatini
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * Filter that changes number of samples on single output operation
25 */
26
27 #include "libavutil/channel_layout.h"
28 #include "libavutil/opt.h"
29 #include "avfilter.h"
30 #include "audio.h"
31 #include "filters.h"
32 #include "internal.h"
33
34 typedef struct ASNSContext {
35 const AVClass *class;
36 int nb_out_samples; ///< how many samples to output
37 int pad;
38 } ASNSContext;
39
40 #define OFFSET(x) offsetof(ASNSContext, x)
41 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
42
43 static const AVOption asetnsamples_options[] = {
44 { "nb_out_samples", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
45 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
46 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
47 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
48 { NULL }
49 };
50
51 AVFILTER_DEFINE_CLASS(asetnsamples);
52
53 2293 static int activate(AVFilterContext *ctx)
54 {
55 2293 AVFilterLink *inlink = ctx->inputs[0];
56 2293 AVFilterLink *outlink = ctx->outputs[0];
57 2293 ASNSContext *s = ctx->priv;
58 2293 AVFrame *frame = NULL, *pad_frame;
59 int ret;
60
61
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2293 times.
2293 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
62
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2293 times.
2293 if (ctx->is_disabled)
64 ret = ff_inlink_consume_frame(inlink, &frame);
65 else
66 2293 ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
67
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2293 times.
2293 if (ret < 0)
68 return ret;
69
70
2/2
✓ Branch 0 taken 1556 times.
✓ Branch 1 taken 737 times.
2293 if (ret > 0) {
71
5/6
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 776 times.
✓ Branch 2 taken 780 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 778 times.
✓ Branch 5 taken 2 times.
1556 if (!s->pad || ctx->is_disabled || frame->nb_samples == s->nb_out_samples)
72 1554 return ff_filter_frame(outlink, frame);
73
74 2 pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pad_frame) {
76 av_frame_free(&frame);
77 return AVERROR(ENOMEM);
78 }
79
80 2 ret = av_frame_copy_props(pad_frame, frame);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
82 av_frame_free(&pad_frame);
83 av_frame_free(&frame);
84 return ret;
85 }
86
87 2 av_samples_copy(pad_frame->extended_data, frame->extended_data,
88 2 0, 0, frame->nb_samples, frame->ch_layout.nb_channels, frame->format);
89 2 av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
90 2 s->nb_out_samples - frame->nb_samples, frame->ch_layout.nb_channels,
91 2 frame->format);
92 2 av_frame_free(&frame);
93 2 return ff_filter_frame(outlink, pad_frame);
94 }
95
96
2/2
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 681 times.
737 FF_FILTER_FORWARD_STATUS(inlink, outlink);
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 681 times.
681 if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples) {
98 ff_filter_set_ready(ctx, 100);
99 return 0;
100 }
101
2/2
✓ Branch 1 taken 663 times.
✓ Branch 2 taken 18 times.
681 FF_FILTER_FORWARD_WANTED(outlink, inlink);
102
103 18 return FFERROR_NOT_READY;
104 }
105
106 const AVFilter ff_af_asetnsamples = {
107 .name = "asetnsamples",
108 .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
109 .priv_size = sizeof(ASNSContext),
110 .priv_class = &asetnsamples_class,
111 FILTER_INPUTS(ff_audio_default_filterpad),
112 FILTER_OUTPUTS(ff_audio_default_filterpad),
113 .activate = activate,
114 .flags = AVFILTER_FLAG_SUPPORT_TIMELINE_INTERNAL,
115 .process_command = ff_filter_process_command,
116 };
117