FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/audio.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 53 63 84.1%
Functions: 5 5 100.0%
Branches: 28 42 66.7%

Line Branch Exec Source
1 /*
2 * Copyright (c) Stefano Sabatini | stefasab at gmail.com
3 * Copyright (c) S.N. Hemanth Meenakshisundaram | smeenaks at ucsd.edu
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 #include "libavutil/avassert.h"
23 #include "libavutil/channel_layout.h"
24 #include "libavutil/common.h"
25 #include "libavutil/cpu.h"
26 #include "libavutil/eval.h"
27
28 #include "audio.h"
29 #include "avfilter.h"
30 #include "avfilter_internal.h"
31 #include "filters.h"
32 #include "framepool.h"
33
34 const AVFilterPad ff_audio_default_filterpad[1] = {
35 {
36 .name = "default",
37 .type = AVMEDIA_TYPE_AUDIO,
38 }
39 };
40
41 45 AVFrame *ff_null_get_audio_buffer(AVFilterLink *link, int nb_samples)
42 {
43 45 return ff_get_audio_buffer(link->dst->outputs[0], nb_samples);
44 }
45
46 232565 AVFrame *ff_default_get_audio_buffer(AVFilterLink *link, int nb_samples)
47 {
48 232565 AVFrame *frame = NULL;
49 232565 FilterLinkInternal *const li = ff_link_internal(link);
50 232565 int channels = link->ch_layout.nb_channels;
51 232565 int align = av_cpu_max_align();
52
53
2/2
✓ Branch 0 taken 1689 times.
✓ Branch 1 taken 230876 times.
232565 if (!li->frame_pool) {
54 3378 li->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
55 1689 nb_samples, link->format, align);
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1689 times.
1689 if (!li->frame_pool)
57 return NULL;
58 } else {
59 230876 int pool_channels = 0;
60 230876 int pool_nb_samples = 0;
61 230876 int pool_align = 0;
62 230876 enum AVSampleFormat pool_format = AV_SAMPLE_FMT_NONE;
63
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 230876 times.
230876 if (ff_frame_pool_get_audio_config(li->frame_pool,
65 &pool_channels, &pool_nb_samples,
66 &pool_format, &pool_align) < 0) {
67 return NULL;
68 }
69
70
3/4
✓ Branch 0 taken 230876 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 229322 times.
✓ Branch 3 taken 1554 times.
230876 if (pool_channels != channels || pool_nb_samples < nb_samples ||
71
2/4
✓ Branch 0 taken 229322 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 229322 times.
229322 pool_format != link->format || pool_align != align) {
72
73 1554 ff_frame_pool_uninit(&li->frame_pool);
74 3108 li->frame_pool = ff_frame_pool_audio_init(av_buffer_allocz, channels,
75 1554 nb_samples, link->format, align);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1554 times.
1554 if (!li->frame_pool)
77 return NULL;
78 }
79 }
80
81 232565 frame = ff_frame_pool_get(li->frame_pool);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 232565 times.
232565 if (!frame)
83 return NULL;
84
85 232565 frame->nb_samples = nb_samples;
86
3/4
✓ Branch 0 taken 230553 times.
✓ Branch 1 taken 2012 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 230553 times.
463118 if (link->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC &&
87 230553 av_channel_layout_copy(&frame->ch_layout, &link->ch_layout) < 0) {
88 av_frame_free(&frame);
89 return NULL;
90 }
91 232565 frame->sample_rate = link->sample_rate;
92
93 232565 av_samples_set_silence(frame->extended_data, 0, nb_samples, channels, link->format);
94
95 232565 return frame;
96 }
97
98 232645 AVFrame *ff_get_audio_buffer(AVFilterLink *link, int nb_samples)
99 {
100 232645 AVFrame *ret = NULL;
101
102
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 232248 times.
232645 if (link->dstpad->get_buffer.audio)
103 397 ret = link->dstpad->get_buffer.audio(link, nb_samples);
104
105
2/2
✓ Branch 0 taken 232248 times.
✓ Branch 1 taken 397 times.
232645 if (!ret)
106 232248 ret = ff_default_get_audio_buffer(link, nb_samples);
107
108 232645 return ret;
109 }
110
111 15 int ff_parse_sample_rate(int *ret, const char *arg, void *log_ctx)
112 {
113 char *tail;
114 15 double srate = av_strtod(arg, &tail);
115
4/8
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 15 times.
15 if (*tail || srate < 1 || (int)srate != srate || srate > INT_MAX) {
116 av_log(log_ctx, AV_LOG_ERROR, "Invalid sample rate '%s'\n", arg);
117 return AVERROR(EINVAL);
118 }
119 15 *ret = srate;
120 15 return 0;
121 }
122
123 65 int ff_parse_channel_layout(AVChannelLayout *ret, int *nret, const char *arg,
124 void *log_ctx)
125 {
126 65 AVChannelLayout chlayout = { 0 };
127 int res;
128
129 65 res = av_channel_layout_from_string(&chlayout, arg);
130
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 60 times.
65 if (res < 0) {
131 5 av_log(log_ctx, AV_LOG_ERROR, "Invalid channel layout '%s'\n", arg);
132 5 return AVERROR(EINVAL);
133 }
134
135
3/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13 times.
60 if (chlayout.order == AV_CHANNEL_ORDER_UNSPEC && !nret) {
136 av_log(log_ctx, AV_LOG_ERROR, "Unknown channel layout '%s' is not supported.\n", arg);
137 return AVERROR(EINVAL);
138 }
139 60 *ret = chlayout;
140
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (nret)
141 60 *nret = chlayout.nb_channels;
142
143 60 return 0;
144 }
145