FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/af_asetnsamples.c
Date: 2022-12-05 20:26:17
Exec Total Coverage
Lines: 25 34 73.5%
Functions: 1 1 100.0%
Branches: 15 20 75.0%

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 #include "formats.h"
34
35 typedef struct ASNSContext {
36 const AVClass *class;
37 int nb_out_samples; ///< how many samples to output
38 int pad;
39 } ASNSContext;
40
41 #define OFFSET(x) offsetof(ASNSContext, x)
42 #define FLAGS AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
43
44 static const AVOption asetnsamples_options[] = {
45 { "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 },
46 { "n", "set the number of per-frame output samples", OFFSET(nb_out_samples), AV_OPT_TYPE_INT, {.i64=1024}, 1, INT_MAX, FLAGS },
47 { "pad", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
48 { "p", "pad last frame with zeros", OFFSET(pad), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS },
49 { NULL }
50 };
51
52 AVFILTER_DEFINE_CLASS(asetnsamples);
53
54 1565 static int activate(AVFilterContext *ctx)
55 {
56 1565 AVFilterLink *inlink = ctx->inputs[0];
57 1565 AVFilterLink *outlink = ctx->outputs[0];
58 1565 ASNSContext *s = ctx->priv;
59 1565 AVFrame *frame = NULL, *pad_frame;
60 int ret;
61
62
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1564 times.
1565 FF_FILTER_FORWARD_STATUS_BACK(outlink, inlink);
63
64 1564 ret = ff_inlink_consume_samples(inlink, s->nb_out_samples, s->nb_out_samples, &frame);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1564 times.
1564 if (ret < 0)
66 return ret;
67
68
2/2
✓ Branch 0 taken 1037 times.
✓ Branch 1 taken 527 times.
1564 if (ret > 0) {
69
4/4
✓ Branch 0 taken 520 times.
✓ Branch 1 taken 517 times.
✓ Branch 2 taken 518 times.
✓ Branch 3 taken 2 times.
1037 if (!s->pad || frame->nb_samples == s->nb_out_samples)
70 1035 return ff_filter_frame(outlink, frame);
71
72 2 pad_frame = ff_get_audio_buffer(outlink, s->nb_out_samples);
73
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!pad_frame) {
74 av_frame_free(&frame);
75 return AVERROR(ENOMEM);
76 }
77
78 2 ret = av_frame_copy_props(pad_frame, frame);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
80 av_frame_free(&pad_frame);
81 av_frame_free(&frame);
82 return ret;
83 }
84
85 2 av_samples_copy(pad_frame->extended_data, frame->extended_data,
86 2 0, 0, frame->nb_samples, frame->ch_layout.nb_channels, frame->format);
87 2 av_samples_set_silence(pad_frame->extended_data, frame->nb_samples,
88 2 s->nb_out_samples - frame->nb_samples, frame->ch_layout.nb_channels,
89 2 frame->format);
90 2 av_frame_free(&frame);
91 2 return ff_filter_frame(outlink, pad_frame);
92 }
93
94
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 524 times.
527 FF_FILTER_FORWARD_STATUS(inlink, outlink);
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 524 times.
524 if (ff_inlink_queued_samples(inlink) >= s->nb_out_samples) {
96 ff_filter_set_ready(ctx, 100);
97 return 0;
98 }
99
1/2
✓ Branch 1 taken 524 times.
✗ Branch 2 not taken.
524 FF_FILTER_FORWARD_WANTED(outlink, inlink);
100
101 return FFERROR_NOT_READY;
102 }
103
104 static const AVFilterPad asetnsamples_inputs[] = {
105 {
106 .name = "default",
107 .type = AVMEDIA_TYPE_AUDIO,
108 },
109 };
110
111 static const AVFilterPad asetnsamples_outputs[] = {
112 {
113 .name = "default",
114 .type = AVMEDIA_TYPE_AUDIO,
115 },
116 };
117
118 const AVFilter ff_af_asetnsamples = {
119 .name = "asetnsamples",
120 .description = NULL_IF_CONFIG_SMALL("Set the number of samples for each output audio frames."),
121 .priv_size = sizeof(ASNSContext),
122 .priv_class = &asetnsamples_class,
123 FILTER_INPUTS(asetnsamples_inputs),
124 FILTER_OUTPUTS(asetnsamples_outputs),
125 .activate = activate,
126 };
127