FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswresample/swresample.c
Date: 2026-08-21 21:49:44
Exec Total Coverage
Lines: 472 619 76.3%
Functions: 23 24 95.8%
Branches: 352 529 66.5%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
3 *
4 * This file is part of libswresample
5 *
6 * libswresample 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 * libswresample 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 libswresample; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/mem.h"
22 #include "libavutil/opt.h"
23 #include "swresample_internal.h"
24 #include "audioconvert.h"
25 #include "libavutil/avassert.h"
26 #include "libavutil/channel_layout.h"
27 #include "libavutil/internal.h"
28
29 #include <float.h>
30
31 #define ALIGN 32
32
33 6398 int swri_check_chlayout(struct SwrContext *s, const AVChannelLayout *chl, const char *name) {
34 char l1[1024];
35 int ret;
36
37
2/4
✓ Branch 1 taken 6398 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6398 times.
6398 if (!(ret = av_channel_layout_check(chl)) || chl->nb_channels > SWR_CH_MAX) {
38 if (ret)
39 av_channel_layout_describe(chl, l1, sizeof(l1));
40 av_log(s, AV_LOG_WARNING, "%s channel layout \"%s\" is invalid or unsupported.\n", name, ret ? l1 : "");
41 return AVERROR(EINVAL);
42 }
43
44 6398 return 0;
45 }
46
47 18 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
48
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if(!s || s->in_convert) // s needs to be allocated but not initialized
49 return AVERROR(EINVAL);
50 18 s->channel_map = channel_map;
51 18 return 0;
52 }
53
54 1576 int swr_alloc_set_opts2(struct SwrContext **ps,
55 const AVChannelLayout *out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
56 const AVChannelLayout *in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
57 int log_offset, void *log_ctx) {
58 1576 struct SwrContext *s = *ps;
59 int ret;
60
61
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1550 times.
1576 if (!s) s = swr_alloc();
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1576 times.
1576 if (!s) return AVERROR(ENOMEM);
63
64 1576 *ps = s;
65
66 1576 s->log_level_offset = log_offset;
67 1576 s->log_ctx = log_ctx;
68
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_chlayout(s, "ochl", out_ch_layout, 0)) < 0)
70 goto fail;
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = swri_check_chlayout(s, out_ch_layout, "ochl")) < 0)
72 goto fail;
73
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_int(s, "osf", out_sample_fmt, 0)) < 0)
75 goto fail;
76
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_int(s, "osr", out_sample_rate, 0)) < 0)
78 goto fail;
79
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_chlayout(s, "ichl", in_ch_layout, 0)) < 0)
81 goto fail;
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = swri_check_chlayout(s, in_ch_layout, "ichl")) < 0)
83 goto fail;
84
85
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_int(s, "isf", in_sample_fmt, 0)) < 0)
86 goto fail;
87
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1576 times.
1576 if ((ret = av_opt_set_int(s, "isr", in_sample_rate, 0)) < 0)
89 goto fail;
90
91 1576 return 0;
92 fail:
93 av_log(s, AV_LOG_ERROR, "Failed to set option\n");
94 swr_free(ps);
95 return ret;
96 }
97
98 6074 static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
99 6074 a->fmt = fmt;
100 6074 a->bps = av_get_bytes_per_sample(fmt);
101 6074 a->planar= av_sample_fmt_is_planar(fmt);
102
2/2
✓ Branch 0 taken 4636 times.
✓ Branch 1 taken 1438 times.
6074 if (a->ch_count == 1)
103 4636 a->planar = 1;
104 6074 }
105
106 33688 static void free_temp(AudioData *a){
107 33688 av_free(a->data);
108 33688 memset(a, 0, sizeof(*a));
109 33688 }
110
111 4211 static void clear_context(SwrContext *s){
112 4211 s->in_buffer_index= 0;
113 4211 s->in_buffer_count= 0;
114 4211 s->resample_in_constraint= 0;
115 4211 memset(s->in.ch, 0, sizeof(s->in.ch));
116 4211 memset(s->out.ch, 0, sizeof(s->out.ch));
117 4211 free_temp(&s->postin);
118 4211 free_temp(&s->midbuf);
119 4211 free_temp(&s->preout);
120 4211 free_temp(&s->in_buffer);
121 4211 free_temp(&s->silence);
122 4211 free_temp(&s->drop_temp);
123 4211 free_temp(&s->dither.noise);
124 4211 free_temp(&s->dither.temp);
125 4211 av_channel_layout_uninit(&s->in_ch_layout);
126 4211 av_channel_layout_uninit(&s->out_ch_layout);
127 4211 av_channel_layout_uninit(&s->used_ch_layout);
128 4211 swri_audio_convert_free(&s-> in_convert);
129 4211 swri_audio_convert_free(&s->out_convert);
130 4211 swri_audio_convert_free(&s->full_convert);
131 4211 swri_rematrix_free(s);
132
133 4211 s->delayed_samples_fixup = 0;
134 4211 s->flushed = 0;
135 4211 }
136
137 2610 av_cold void swr_free(SwrContext **ss){
138 2610 SwrContext *s= *ss;
139
2/2
✓ Branch 0 taken 2581 times.
✓ Branch 1 taken 29 times.
2610 if(s){
140 2581 clear_context(s);
141 2581 av_channel_layout_uninit(&s->user_in_chlayout);
142 2581 av_channel_layout_uninit(&s->user_out_chlayout);
143 2581 av_channel_layout_uninit(&s->user_used_chlayout);
144
145
2/2
✓ Branch 0 taken 1599 times.
✓ Branch 1 taken 982 times.
2581 if (s->resampler)
146 1599 s->resampler->free(&s->resample);
147 }
148
149 2610 av_freep(ss);
150 2610 }
151
152 17 av_cold void swr_close(SwrContext *s){
153 17 clear_context(s);
154 17 }
155
156 1613 av_cold int swr_init(struct SwrContext *s){
157 int ret;
158 char l1[1024], l2[1024];
159
160 1613 clear_context(s);
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if((unsigned) s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
163 av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt);
164 return AVERROR(EINVAL);
165 }
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if((unsigned) s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
167 av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt);
168 return AVERROR(EINVAL);
169 }
170
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if(s-> in_sample_rate <= 0){
172 av_log(s, AV_LOG_ERROR, "Requested input sample rate %d is invalid\n", s->in_sample_rate);
173 return AVERROR(EINVAL);
174 }
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if(s->out_sample_rate <= 0){
176 av_log(s, AV_LOG_ERROR, "Requested output sample rate %d is invalid\n", s->out_sample_rate);
177 return AVERROR(EINVAL);
178 }
179
180 1613 s->out.ch_count = s-> user_out_chlayout.nb_channels;
181 1613 s-> in.ch_count = s-> user_in_chlayout.nb_channels;
182
183
2/4
✓ Branch 1 taken 1613 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1613 times.
3226 if (swri_check_chlayout(s, &s->user_in_chlayout , "input") ||
184 1613 swri_check_chlayout(s, &s->user_out_chlayout, "output"))
185 return AVERROR(EINVAL);
186
187 1613 ret = av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
188 1613 ret |= av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
189 1613 ret |= av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
190
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if (ret < 0)
191 return ret;
192
193 1613 s->int_sample_fmt= s->user_int_sample_fmt;
194
195 1613 s->dither.method = s->user_dither_method;
196
197
1/2
✓ Branch 0 taken 1613 times.
✗ Branch 1 not taken.
1613 switch(s->engine){
198 #if CONFIG_LIBSOXR
199 case SWR_ENGINE_SOXR: s->resampler = &swri_soxr_resampler; break;
200 #endif
201 1613 case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
202 default:
203 av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
204 return AVERROR(EINVAL);
205 }
206
207
2/2
✓ Branch 1 taken 1595 times.
✓ Branch 2 taken 18 times.
1613 if (!av_channel_layout_check(&s->used_ch_layout))
208 1595 av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
209
210
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1605 times.
1613 if (s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels)
211 8 av_channel_layout_uninit(&s->in_ch_layout);
212
213
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1612 times.
1613 if (s->used_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
214 1 av_channel_layout_default(&s->used_ch_layout, s->used_ch_layout.nb_channels);
215
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1587 times.
1613 if (s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
216 26 ret = av_channel_layout_copy(&s->in_ch_layout, &s->used_ch_layout);
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (ret < 0)
218 return ret;
219 }
220
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1594 times.
1613 if (s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
221 19 av_channel_layout_default(&s->out_ch_layout, s->out.ch_count);
222
223 1613 s->rematrix = av_channel_layout_compare(&s->out_ch_layout, &s->in_ch_layout) ||
224
3/4
✓ Branch 0 taken 1585 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1585 times.
✗ Branch 3 not taken.
3198 s->rematrix_volume!=1.0 ||
225
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1583 times.
1585 s->rematrix_custom;
226
227
2/2
✓ Branch 0 taken 972 times.
✓ Branch 1 taken 641 times.
1613 if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
228 // 16bit or less to 16bit or less with the same sample rate
229
2/2
✓ Branch 1 taken 569 times.
✓ Branch 2 taken 403 times.
972 if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
230
2/2
✓ Branch 1 taken 491 times.
✓ Branch 2 taken 78 times.
569 && av_get_bytes_per_sample(s->out_sample_fmt) <= 2
231
2/2
✓ Branch 0 taken 490 times.
✓ Branch 1 taken 1 times.
491 && s->out_sample_rate==s->in_sample_rate) {
232 490 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
233 // 8 -> 8, 16->8, 8->16bit
234 482 } else if( av_get_bytes_per_sample(s-> in_sample_fmt)
235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 482 times.
482 +av_get_bytes_per_sample(s->out_sample_fmt) <= 3 ) {
236 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
237
2/2
✓ Branch 1 taken 79 times.
✓ Branch 2 taken 403 times.
482 }else if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
238
2/2
✓ Branch 0 taken 77 times.
✓ Branch 1 taken 2 times.
79 && !s->rematrix
239
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 1 times.
77 && s->out_sample_rate==s->in_sample_rate
240
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 && !(s->flags & SWR_FLAG_RESAMPLE)){
241 76 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
242
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 330 times.
406 }else if( av_get_planar_sample_fmt(s-> in_sample_fmt) == AV_SAMPLE_FMT_S32P
243
2/2
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 34 times.
76 && av_get_planar_sample_fmt(s->out_sample_fmt) == AV_SAMPLE_FMT_S32P
244
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && !s->rematrix
245
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && s->out_sample_rate == s->in_sample_rate
246
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && !(s->flags & SWR_FLAG_RESAMPLE)
247
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && s->engine != SWR_ENGINE_SOXR){
248 42 s->int_sample_fmt= AV_SAMPLE_FMT_S32P;
249
2/2
✓ Branch 1 taken 321 times.
✓ Branch 2 taken 43 times.
364 }else if(av_get_bytes_per_sample(s->in_sample_fmt) <= 4){
250 321 s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
251 }else{
252 43 s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
253 }
254 }
255 1613 av_log(s, AV_LOG_DEBUG, "Using %s internally between filters\n", av_get_sample_fmt_name(s->int_sample_fmt));
256
257
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 742 times.
1613 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
258
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 187 times.
871 &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
259
1/2
✓ Branch 0 taken 684 times.
✗ Branch 1 not taken.
684 &&s->int_sample_fmt != AV_SAMPLE_FMT_S64P
260
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 485 times.
684 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
262 av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, s16p/s32p/s64p/fltp/dblp are supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
263 return AVERROR(EINVAL);
264 }
265
266 1613 set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
267 1613 set_audiodata_fmt(&s->out, s->out_sample_fmt);
268
269
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1558 times.
1613 if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
270
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
55 if (!s->async && s->min_compensation >= FLT_MAX/2)
271 s->async = 1;
272
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1 times.
55 if (s->firstpts == AV_NOPTS_VALUE)
273 54 s->firstpts =
274 54 s->outpts = s->firstpts_in_samples * s->out_sample_rate;
275 } else
276 1558 s->firstpts = AV_NOPTS_VALUE;
277
278
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1559 times.
1613 if (s->async) {
279
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 1 times.
54 if (s->min_compensation >= FLT_MAX/2)
280 53 s->min_compensation = 0.001;
281
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (s->async > 1.0001) {
282 54 s->max_soft_compensation = s->async / (double) s->in_sample_rate;
283 }
284 }
285
286
4/4
✓ Branch 0 taken 929 times.
✓ Branch 1 taken 684 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 928 times.
1613 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
287 685 s->resample = s->resampler->init(s->resample, s->out_sample_rate, s->in_sample_rate, s->filter_size, s->phase_shift, s->linear_interp, s->cutoff, s->int_sample_fmt, s->filter_type, s->kaiser_beta, s->precision, s->cheby, s->exact_rational);
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 685 times.
685 if (!s->resample) {
289 av_log(s, AV_LOG_ERROR, "Failed to initialize resampler\n");
290 return AVERROR(ENOMEM);
291 }
292 }else
293 928 s->resampler->free(&s->resample);
294
2/2
✓ Branch 0 taken 871 times.
✓ Branch 1 taken 742 times.
1613 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
295
2/2
✓ Branch 0 taken 684 times.
✓ Branch 1 taken 187 times.
871 && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
296
2/2
✓ Branch 0 taken 199 times.
✓ Branch 1 taken 485 times.
684 && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 199 times.
199 && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
298 && s->resample){
299 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16p/s32p/fltp/dblp\n");
300 ret = AVERROR(EINVAL);
301 goto fail;
302 }
303
304 #define RSC 1 //FIXME finetune
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if(!s-> in.ch_count)
306 s-> in.ch_count = s->in_ch_layout.nb_channels;
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1613 times.
1613 if (!av_channel_layout_check(&s->used_ch_layout))
308 av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if(!s->out.ch_count)
310 s->out.ch_count = s->out_ch_layout.nb_channels;
311
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 if(!s-> in.ch_count){
313 av_assert0(s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
314 av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
315 ret = AVERROR(EINVAL);
316 goto fail;
317 }
318
319 1613 av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
320 1613 av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
321
3/4
✓ Branch 0 taken 1612 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1612 times.
1613 if (s->in_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels) {
322 av_log(s, AV_LOG_ERROR, "Input channel layout %s mismatches specified channel count %d\n", l1, s->used_ch_layout.nb_channels);
323 ret = AVERROR(EINVAL);
324 goto fail;
325 }
326
327
2/2
✓ Branch 0 taken 1612 times.
✓ Branch 1 taken 1 times.
1613 if (( s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC
328
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1611 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
1613 || s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) && s->used_ch_layout.nb_channels != s->out.ch_count && !s->rematrix_custom) {
329 av_log(s, AV_LOG_ERROR, "Rematrix is needed between %s and %s "
330 "but there is not enough information to do it\n", l1, l2);
331 ret = AVERROR(EINVAL);
332 goto fail;
333 }
334
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 av_assert0(s->used_ch_layout.nb_channels);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1613 times.
1613 av_assert0(s->out.ch_count);
337 1613 s->resample_first= RSC*s->out.ch_count/s->used_ch_layout.nb_channels - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
338
339 1613 s->in_buffer= s->in;
340 1613 s->silence = s->in;
341 1613 s->drop_temp= s->out;
342
343
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1613 times.
1613 if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0)
344 goto fail;
345
346
7/8
✓ Branch 0 taken 928 times.
✓ Branch 1 taken 685 times.
✓ Branch 2 taken 902 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 892 times.
✓ Branch 5 taken 10 times.
✓ Branch 6 taken 892 times.
✗ Branch 7 not taken.
1613 if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
347 892 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
348 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
349 892 return 0;
350 }
351
352 721 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
353 s-> in_sample_fmt, s->used_ch_layout.nb_channels, s->channel_map, 0);
354 721 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
355 s->int_sample_fmt, s->out.ch_count, NULL, 0);
356
357
2/4
✓ Branch 0 taken 721 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 721 times.
721 if (!s->in_convert || !s->out_convert) {
358 ret = AVERROR(ENOMEM);
359 goto fail;
360 }
361
362 721 s->postin= s->in;
363 721 s->preout= s->out;
364 721 s->midbuf= s->in;
365
366
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 703 times.
721 if(s->channel_map){
367 18 s->postin.ch_count=
368 18 s->midbuf.ch_count= s->used_ch_layout.nb_channels;
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if(s->resample)
370 s->in_buffer.ch_count= s->used_ch_layout.nb_channels;
371 }
372
2/2
✓ Branch 0 taken 351 times.
✓ Branch 1 taken 370 times.
721 if(!s->resample_first){
373 351 s->midbuf.ch_count= s->out.ch_count;
374
2/2
✓ Branch 0 taken 327 times.
✓ Branch 1 taken 24 times.
351 if(s->resample)
375 327 s->in_buffer.ch_count = s->out.ch_count;
376 }
377
378 721 set_audiodata_fmt(&s->postin, s->int_sample_fmt);
379 721 set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
380 721 set_audiodata_fmt(&s->preout, s->int_sample_fmt);
381
382
2/2
✓ Branch 0 taken 685 times.
✓ Branch 1 taken 36 times.
721 if(s->resample){
383 685 set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
384 }
385
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
721 av_assert0(!s->preout.count);
387 721 s->dither.noise = s->preout;
388 721 s->dither.temp = s->preout;
389
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
721 if (s->dither.method > SWR_DITHER_NS) {
390 s->dither.noise.bps = 4;
391 s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
392 s->dither.noise_scale = 1;
393 }
394
395
3/4
✓ Branch 0 taken 691 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 691 times.
721 if(s->rematrix || s->dither.method) {
396 30 ret = swri_rematrix_init(s);
397
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
398 goto fail;
399 }
400
401 721 return 0;
402 fail:
403 swr_close(s);
404 return ret;
405
406 }
407
408 100459 int swri_realloc_audio(AudioData *a, int count){
409 int i, countb;
410 AudioData old;
411
412
2/4
✓ Branch 0 taken 100459 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 100459 times.
100459 if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
413 return AVERROR(EINVAL);
414
415
2/2
✓ Branch 0 taken 96396 times.
✓ Branch 1 taken 4063 times.
100459 if(a->count >= count)
416 96396 return 0;
417
418 4063 count*=2;
419
420 4063 countb= FFALIGN(count*a->bps, ALIGN);
421 4063 old= *a;
422
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4063 times.
4063 av_assert0(a->bps);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4063 times.
4063 av_assert0(a->ch_count);
425
426 4063 a->data = av_calloc(countb, a->ch_count);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4063 times.
4063 if(!a->data)
428 return AVERROR(ENOMEM);
429
2/2
✓ Branch 0 taken 4582 times.
✓ Branch 1 taken 4063 times.
8645 for(i=0; i<a->ch_count; i++){
430
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4582 times.
4582 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
431
3/4
✓ Branch 0 taken 1246 times.
✓ Branch 1 taken 3336 times.
✓ Branch 2 taken 1246 times.
✗ Branch 3 not taken.
4582 if(a->count && a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
432 }
433
3/4
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 2905 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1158 times.
4063 if(a->count && !a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
434 4063 av_freep(&old.data);
435 4063 a->count= count;
436
437 4063 return 1;
438 }
439
440 35414 static void copy(AudioData *out, AudioData *in,
441 int count){
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35414 times.
35414 av_assert0(out->planar == in->planar);
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35414 times.
35414 av_assert0(out->bps == in->bps);
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35414 times.
35414 av_assert0(out->ch_count == in->ch_count);
445
1/2
✓ Branch 0 taken 35414 times.
✗ Branch 1 not taken.
35414 if(out->planar){
446 int ch;
447
2/2
✓ Branch 0 taken 56027 times.
✓ Branch 1 taken 35414 times.
91441 for(ch=0; ch<out->ch_count; ch++)
448 56027 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
449 }else
450 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
451 35414 }
452
453 554509 static void fill_audiodata(AudioData *out, uint8_t *const in_arg [SWR_CH_MAX])
454 {
455 int i;
456
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 554441 times.
554509 if(!in_arg){
457 68 memset(out->ch, 0, sizeof(out->ch));
458
2/2
✓ Branch 0 taken 401509 times.
✓ Branch 1 taken 152932 times.
554441 }else if(out->planar){
459
2/2
✓ Branch 0 taken 613520 times.
✓ Branch 1 taken 401509 times.
1015029 for(i=0; i<out->ch_count; i++)
460 613520 out->ch[i]= in_arg[i];
461 }else{
462
2/2
✓ Branch 0 taken 359626 times.
✓ Branch 1 taken 152932 times.
512558 for(i=0; i<out->ch_count; i++)
463 359626 out->ch[i]= in_arg[0] + i*out->bps;
464 }
465 554509 }
466
467 137 static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
468 int i;
469
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 if(out->planar){
470
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 137 times.
274 for(i=0; i<out->ch_count; i++)
471 137 in_arg[i]= out->ch[i];
472 }else{
473 in_arg[0]= out->ch[0];
474 }
475 137 }
476
477 /**
478 *
479 * out may be equal in.
480 */
481 477127 static void buf_set(AudioData *out, AudioData *in, int count){
482 int ch;
483
2/2
✓ Branch 0 taken 466373 times.
✓ Branch 1 taken 10754 times.
477127 if(in->planar){
484
2/2
✓ Branch 0 taken 780315 times.
✓ Branch 1 taken 466373 times.
1246688 for(ch=0; ch<out->ch_count; ch++)
485 780315 out->ch[ch]= in->ch[ch] + count*out->bps;
486 }else{
487
2/2
✓ Branch 0 taken 27928 times.
✓ Branch 1 taken 10754 times.
38682 for(ch=out->ch_count-1; ch>=0; ch--)
488 27928 out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
489 }
490 477127 }
491
492 /**
493 *
494 * @return number of samples output per channel
495 */
496 19606 static int resample(SwrContext *s, AudioData *out_param, int out_count,
497 const AudioData * in_param, int in_count){
498 AudioData in, out, tmp;
499 19606 int ret_sum=0;
500 19606 int border=0;
501
1/2
✓ Branch 0 taken 19606 times.
✗ Branch 1 not taken.
19606 int padless = ARCH_X86 && s->engine == SWR_ENGINE_SWR ? 7 : 0;
502
503 av_assert1(s->in_buffer.ch_count == in_param->ch_count);
504 av_assert1(s->in_buffer.planar == in_param->planar);
505 av_assert1(s->in_buffer.fmt == in_param->fmt);
506
507 19606 tmp=out=*out_param;
508 19606 in = *in_param;
509
510 19606 border = s->resampler->invert_initial_buffer(s->resample, &s->in_buffer,
511 &in, in_count, &s->in_buffer_index, &s->in_buffer_count);
512
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 19519 times.
19606 if (border == INT_MAX) {
513 87 return 0;
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19519 times.
19519 } else if (border < 0) {
515 return border;
516
2/2
✓ Branch 0 taken 683 times.
✓ Branch 1 taken 18836 times.
19519 } else if (border) {
517 683 buf_set(&in, &in, border);
518 683 in_count -= border;
519 683 s->resample_in_constraint = 0;
520 }
521
522 35379 do{
523 int ret, size, consumed;
524
4/4
✓ Branch 0 taken 46898 times.
✓ Branch 1 taken 8000 times.
✓ Branch 2 taken 46892 times.
✓ Branch 3 taken 6 times.
54898 if(!s->resample_in_constraint && s->in_buffer_count){
525 46892 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
526 46892 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
527 46892 out_count -= ret;
528 46892 ret_sum += ret;
529 46892 buf_set(&out, &out, ret);
530 46892 s->in_buffer_count -= consumed;
531 46892 s->in_buffer_index += consumed;
532
533
2/2
✓ Branch 0 taken 19514 times.
✓ Branch 1 taken 27378 times.
46892 if(!in_count)
534 19514 break;
535
2/2
✓ Branch 0 taken 17797 times.
✓ Branch 1 taken 9581 times.
27378 if(s->in_buffer_count <= border){
536 17797 buf_set(&in, &in, -s->in_buffer_count);
537 17797 in_count += s->in_buffer_count;
538 17797 s->in_buffer_count=0;
539 17797 s->in_buffer_index=0;
540 17797 border = 0;
541 }
542 }
543
544
6/6
✓ Branch 0 taken 35380 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 35379 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 17841 times.
✓ Branch 5 taken 17542 times.
35384 if((s->flushed || in_count > padless) && !s->in_buffer_count){
545 17841 s->in_buffer_index=0;
546 17841 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
547 17841 out_count -= ret;
548 17841 ret_sum += ret;
549 17841 buf_set(&out, &out, ret);
550 17841 in_count -= consumed;
551 17841 buf_set(&in, &in, consumed);
552 }
553
554 //TODO is this check sane considering the advanced copy avoidance below
555 35384 size= s->in_buffer_index + s->in_buffer_count + in_count;
556
2/2
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 34688 times.
35384 if( size > s->in_buffer.count
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 696 times.
696 && s->in_buffer_count + in_count <= s->in_buffer_index){
558 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
559 copy(&s->in_buffer, &tmp, s->in_buffer_count);
560 s->in_buffer_index=0;
561 }else
562
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35384 times.
35384 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
563 return ret;
564
565
2/2
✓ Branch 0 taken 35379 times.
✓ Branch 1 taken 5 times.
35384 if(in_count){
566 35379 int count= in_count;
567
6/6
✓ Branch 0 taken 17542 times.
✓ Branch 1 taken 17837 times.
✓ Branch 2 taken 17231 times.
✓ Branch 3 taken 311 times.
✓ Branch 4 taken 17211 times.
✓ Branch 5 taken 20 times.
35379 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
568
569 35379 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
570 35379 copy(&tmp, &in, /*in_*/count);
571 35379 s->in_buffer_count += count;
572 35379 in_count -= count;
573 35379 border += count;
574 35379 buf_set(&in, &in, count);
575 35379 s->resample_in_constraint= 0;
576
3/4
✓ Branch 0 taken 17837 times.
✓ Branch 1 taken 17542 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 17837 times.
35379 if(s->in_buffer_count != count || in_count)
577 35379 continue;
578
1/2
✓ Branch 0 taken 17837 times.
✗ Branch 1 not taken.
17837 if (padless) {
579 17837 padless = 0;
580 17837 continue;
581 }
582 }
583 5 break;
584 }while(1);
585
586 19519 s->resample_in_constraint= !!out_count;
587
588 19519 return ret_sum;
589 }
590
591 277869 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
592 AudioData *in , int in_count){
593 AudioData *postin, *midbuf, *preout;
594 int ret/*, in_max*/;
595 AudioData preout_tmp, midbuf_tmp;
596
597
2/2
✓ Branch 0 taken 256711 times.
✓ Branch 1 taken 21158 times.
277869 if(s->full_convert){
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 256711 times.
256711 av_assert0(!s->resample);
599 256711 swri_audio_convert(s->full_convert, out, in, in_count);
600 256711 return out_count;
601 }
602
603 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
604 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
605
606
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21158 times.
21158 if((ret=swri_realloc_audio(&s->postin, in_count))<0)
607 return ret;
608
2/2
✓ Branch 0 taken 14808 times.
✓ Branch 1 taken 6350 times.
21158 if(s->resample_first){
609
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14808 times.
14808 av_assert0(s->midbuf.ch_count == s->used_ch_layout.nb_channels);
610
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14808 times.
14808 if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
611 return ret;
612 }else{
613
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6350 times.
6350 av_assert0(s->midbuf.ch_count == s->out.ch_count);
614
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6350 times.
6350 if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
615 return ret;
616 }
617
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21158 times.
21158 if((ret=swri_realloc_audio(&s->preout, out_count))<0)
618 return ret;
619
620 21158 postin= &s->postin;
621
622 21158 midbuf_tmp= s->midbuf;
623 21158 midbuf= &midbuf_tmp;
624 21158 preout_tmp= s->preout;
625 21158 preout= &preout_tmp;
626
627
4/6
✓ Branch 0 taken 14988 times.
✓ Branch 1 taken 6170 times.
✓ Branch 2 taken 14988 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14988 times.
✗ Branch 5 not taken.
21158 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
628 14988 postin= in;
629
630
4/4
✓ Branch 0 taken 14808 times.
✓ Branch 1 taken 6350 times.
✓ Branch 2 taken 5912 times.
✓ Branch 3 taken 15246 times.
21158 if(s->resample_first ? !s->resample : !s->rematrix)
631 5912 midbuf= postin;
632
633
4/4
✓ Branch 0 taken 14808 times.
✓ Branch 1 taken 6350 times.
✓ Branch 2 taken 15686 times.
✓ Branch 3 taken 5472 times.
21158 if(s->resample_first ? !s->rematrix : !s->resample)
634 15686 preout= midbuf;
635
636
3/4
✓ Branch 0 taken 14029 times.
✓ Branch 1 taken 7129 times.
✓ Branch 2 taken 14029 times.
✗ Branch 3 not taken.
21158 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
637
3/4
✓ Branch 0 taken 1044 times.
✓ Branch 1 taken 12985 times.
✓ Branch 2 taken 1044 times.
✗ Branch 3 not taken.
14029 && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14029 times.
14029 if(preout==in){
639 out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
640 av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
641 copy(out, in, out_count);
642 return out_count;
643 }
644
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14029 times.
14029 else if(preout==postin) preout= midbuf= postin= out;
645
2/2
✓ Branch 0 taken 12986 times.
✓ Branch 1 taken 1043 times.
14029 else if(preout==midbuf) preout= midbuf= out;
646 1043 else preout= out;
647 }
648
649
2/2
✓ Branch 0 taken 6170 times.
✓ Branch 1 taken 14988 times.
21158 if(in != postin){
650 6170 swri_audio_convert(s->in_convert, postin, in, in_count);
651 }
652
653
2/2
✓ Branch 0 taken 14808 times.
✓ Branch 1 taken 6350 times.
21158 if(s->resample_first){
654
2/2
✓ Branch 0 taken 14506 times.
✓ Branch 1 taken 302 times.
14808 if(postin != midbuf)
655
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14506 times.
14506 if ((out_count = resample(s, midbuf, out_count, postin, in_count)) < 0)
656 return out_count;
657
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 14436 times.
14808 if(midbuf != preout)
658 372 swri_rematrix(s, preout, midbuf, out_count, preout==out);
659 }else{
660
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 5610 times.
6350 if(postin != midbuf)
661 740 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
662
2/2
✓ Branch 0 taken 5100 times.
✓ Branch 1 taken 1250 times.
6350 if(midbuf != preout)
663
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5100 times.
5100 if ((out_count = resample(s, preout, out_count, midbuf, in_count)) < 0)
664 return out_count;
665 }
666
667
4/4
✓ Branch 0 taken 7129 times.
✓ Branch 1 taken 14029 times.
✓ Branch 2 taken 6799 times.
✓ Branch 3 taken 330 times.
21158 if(preout != out && out_count){
668 6799 AudioData *conv_src = preout;
669
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6799 times.
6799 if(s->dither.method){
670 int ch;
671 int dither_count= FFMAX(out_count, 1<<16);
672
673 if (preout == in) {
674 conv_src = &s->dither.temp;
675 if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)
676 return ret;
677 }
678
679 if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)
680 return ret;
681 if(ret)
682 for(ch=0; ch<s->dither.noise.ch_count; ch++)
683 if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828U, s->dither.noise.fmt))<0)
684 return ret;
685 av_assert0(s->dither.noise.ch_count == preout->ch_count);
686
687 if(s->dither.noise_pos + out_count > s->dither.noise.count)
688 s->dither.noise_pos = 0;
689
690 if (s->dither.method < SWR_DITHER_NS){
691 if (s->mix_2_1_simd) {
692 int len1= out_count&~15;
693 int off = len1 * preout->bps;
694
695 if(len1)
696 for(ch=0; ch<preout->ch_count; ch++)
697 s->mix_2_1_simd(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, &s->native_simd_one, 0, 0, len1);
698 if(out_count != len1)
699 for(ch=0; ch<preout->ch_count; ch++)
700 s->mix_2_1_f(conv_src->ch[ch] + off, preout->ch[ch] + off, s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos + off, &s->native_one, 0, 0, out_count - len1);
701 } else {
702 for(ch=0; ch<preout->ch_count; ch++)
703 s->mix_2_1_f(conv_src->ch[ch], preout->ch[ch], s->dither.noise.ch[ch] + s->dither.noise.bps * s->dither.noise_pos, &s->native_one, 0, 0, out_count);
704 }
705 } else {
706 switch(s->int_sample_fmt) {
707 case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;
708 case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;
709 case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;
710 case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;
711 }
712 }
713 s->dither.noise_pos += out_count;
714 }
715 //FIXME packed doesn't need more than 1 chan here!
716 6799 swri_audio_convert(s->out_convert, out, conv_src, out_count);
717 }
718 21158 return out_count;
719 }
720
721 314681 int swr_is_initialized(struct SwrContext *s) {
722 314681 return !!s->in_buffer.ch_count;
723 }
724
725 278637 int attribute_align_arg swr_convert(struct SwrContext *s,
726 uint8_t * const *out_arg, int out_count,
727 const uint8_t * const *in_arg, int in_count)
728 {
729 278637 AudioData * in= &s->in;
730 278637 AudioData *out= &s->out;
731
732
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 278637 times.
278637 if (!swr_is_initialized(s)) {
733 av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");
734 return AVERROR(EINVAL);
735 }
736 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
737 int max_output = swr_get_out_samples(s, in_count);
738 #endif
739
740
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 278585 times.
278689 while(s->drop_output > 0){
741 int ret;
742 uint8_t *tmp_arg[SWR_CH_MAX];
743 #define MAX_DROP_STEP 16384
744
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 104 times.
104 if((ret=swri_realloc_audio(&s->drop_temp, FFMIN(s->drop_output, MAX_DROP_STEP)))<0)
745 52 return ret;
746
747 104 reversefill_audiodata(&s->drop_temp, tmp_arg);
748 104 s->drop_output *= -1; //FIXME find a less hackish solution
749
1/2
✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
104 ret = swr_convert(s, tmp_arg, FFMIN(-s->drop_output, MAX_DROP_STEP), in_arg, in_count); //FIXME optimize but this is as good as never called so maybe it doesn't matter
750 104 s->drop_output *= -1;
751 104 in_count = 0;
752
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 52 times.
104 if(ret>0) {
753 52 s->drop_output -= ret;
754
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
52 if (!s->drop_output && !out_arg)
755 return 0;
756 52 continue;
757 }
758
759
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 av_assert0(s->drop_output);
760 52 return 0;
761 }
762
763
2/2
✓ Branch 0 taken 1979 times.
✓ Branch 1 taken 276606 times.
278585 if(!in_arg){
764
2/2
✓ Branch 0 taken 1297 times.
✓ Branch 1 taken 682 times.
1979 if(s->resample){
765
2/2
✓ Branch 0 taken 659 times.
✓ Branch 1 taken 638 times.
1297 if (!s->flushed)
766 659 s->resampler->flush(s);
767 1297 s->resample_in_constraint = 0;
768 1297 s->flushed = 1;
769
1/2
✓ Branch 0 taken 682 times.
✗ Branch 1 not taken.
682 }else if(!s->in_buffer_count){
770 682 return 0;
771 }
772 }else
773 276606 fill_audiodata(in , (void*)in_arg);
774
775 277903 fill_audiodata(out, out_arg);
776
777
2/2
✓ Branch 0 taken 19606 times.
✓ Branch 1 taken 258297 times.
277903 if(s->resample){
778 19606 int ret = swr_convert_internal(s, out, out_count, in, in_count);
779
4/4
✓ Branch 0 taken 18863 times.
✓ Branch 1 taken 743 times.
✓ Branch 2 taken 18811 times.
✓ Branch 3 taken 52 times.
19606 if(ret>0 && !s->drop_output)
780 18811 s->outpts += ret * (int64_t)s->in_sample_rate;
781
782 av_assert2(max_output < 0 || ret <= max_output);
783
784 19606 return ret;
785 }else{
786 258297 AudioData tmp= *in;
787 258297 int ret2=0;
788 int ret, size;
789 258297 size = FFMIN(out_count, s->in_buffer_count);
790
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 258172 times.
258297 if(size){
791 125 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
792 125 ret= swr_convert_internal(s, out, size, &tmp, size);
793
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if(ret<0)
794 return ret;
795 125 ret2= ret;
796 125 s->in_buffer_count -= ret;
797 125 s->in_buffer_index += ret;
798 125 buf_set(out, out, ret);
799 125 out_count -= ret;
800
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 121 times.
125 if(!s->in_buffer_count)
801 4 s->in_buffer_index = 0;
802 }
803
804
2/2
✓ Branch 0 taken 258172 times.
✓ Branch 1 taken 125 times.
258297 if(in_count){
805 258172 size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
806
807
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 258137 times.
258172 if(in_count > out_count) { //FIXME move after swr_convert_internal
808
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 if( size > s->in_buffer.count
809
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
810 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
811 copy(&s->in_buffer, &tmp, s->in_buffer_count);
812 s->in_buffer_index=0;
813 }else
814
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
815 return ret;
816 }
817
818
2/2
✓ Branch 0 taken 258138 times.
✓ Branch 1 taken 34 times.
258172 if(out_count){
819 258138 size = FFMIN(in_count, out_count);
820 258138 ret= swr_convert_internal(s, out, size, in, size);
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 258138 times.
258138 if(ret<0)
822 return ret;
823 258138 buf_set(in, in, ret);
824 258138 in_count -= ret;
825 258138 ret2 += ret;
826 }
827
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 258137 times.
258172 if(in_count){
828 35 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
829 35 copy(&tmp, in, in_count);
830 35 s->in_buffer_count += in_count;
831 }
832 }
833
3/4
✓ Branch 0 taken 258262 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 258262 times.
✗ Branch 3 not taken.
258297 if(ret2>0 && !s->drop_output)
834 258262 s->outpts += ret2 * (int64_t)s->in_sample_rate;
835 av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
836 258297 return ret2;
837 }
838 }
839
840 52 int swr_drop_output(struct SwrContext *s, int count){
841 const uint8_t *tmp_arg[SWR_CH_MAX];
842 52 s->drop_output += count;
843
844
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if(s->drop_output <= 0)
845 return 0;
846
847 52 av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
848 52 return swr_convert(s, NULL, s->drop_output, tmp_arg, 0);
849 }
850
851 33 int swr_inject_silence(struct SwrContext *s, int count){
852 int ret, i;
853 uint8_t *tmp_arg[SWR_CH_MAX];
854
855
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if(count <= 0)
856 return 0;
857
858 #define MAX_SILENCE_STEP 16384
859
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 33 times.
62 while (count > MAX_SILENCE_STEP) {
860
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if ((ret = swr_inject_silence(s, MAX_SILENCE_STEP)) < 0)
861 return ret;
862 29 count -= MAX_SILENCE_STEP;
863 }
864
865
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if((ret=swri_realloc_audio(&s->silence, count))<0)
866 return ret;
867
868
3/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 33 times.
66 if(s->silence.planar) for(i=0; i<s->silence.ch_count; i++) {
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 memset(s->silence.ch[i], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps);
870 } else
871 memset(s->silence.ch[0], s->silence.bps==1 ? 0x80 : 0, count*s->silence.bps*s->silence.ch_count);
872
873 33 reversefill_audiodata(&s->silence, tmp_arg);
874 33 av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
875 33 ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
876 33 return ret;
877 }
878
879 531236 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
880
3/4
✓ Branch 0 taken 531236 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17282 times.
✓ Branch 3 taken 513954 times.
531236 if (s->resampler && s->resample){
881 17282 return s->resampler->get_delay(s, base);
882 }else{
883 513954 return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
884 }
885 }
886
887 int swr_get_out_samples(struct SwrContext *s, int in_samples)
888 {
889 int64_t out_samples;
890
891 if (in_samples < 0)
892 return AVERROR(EINVAL);
893
894 if (s->resampler && s->resample) {
895 if (!s->resampler->get_out_samples)
896 return AVERROR(ENOSYS);
897 out_samples = s->resampler->get_out_samples(s, in_samples);
898 } else {
899 out_samples = s->in_buffer_count + in_samples;
900 av_assert0(s->out_sample_rate == s->in_sample_rate);
901 }
902
903 if (out_samples > INT_MAX)
904 return AVERROR(EINVAL);
905
906 return out_samples;
907 }
908
909 127 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
910 int ret;
911
912
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 127 times.
127 if (!s || compensation_distance < 0)
913 return AVERROR(EINVAL);
914
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
127 if (!compensation_distance && sample_delta)
915 return AVERROR(EINVAL);
916
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 126 times.
127 if (!s->resample) {
917 1 s->flags |= SWR_FLAG_RESAMPLE;
918 1 ret = swr_init(s);
919
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
920 return ret;
921 }
922
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (!s->resampler->set_compensation){
923 return AVERROR(EINVAL);
924 }else{
925 127 return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
926 }
927 }
928
929 267707 int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
930
2/2
✓ Branch 0 taken 2089 times.
✓ Branch 1 taken 265618 times.
267707 if(pts == INT64_MIN)
931 2089 return s->outpts;
932
933
2/2
✓ Branch 0 taken 1495 times.
✓ Branch 1 taken 264123 times.
265618 if (s->firstpts == AV_NOPTS_VALUE)
934 1495 s->outpts = s->firstpts = pts;
935
936
2/2
✓ Branch 0 taken 264387 times.
✓ Branch 1 taken 1231 times.
265618 if(s->min_compensation >= FLT_MAX) {
937 264387 return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
938 } else {
939 1231 int64_t delta = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate) - s->outpts + s->drop_output*(int64_t)s->in_sample_rate;
940 1231 double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
941
942
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 1048 times.
1231 if(fabs(fdelta) > s->min_compensation) {
943
4/4
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 54 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 127 times.
239 if(s->outpts == s->firstpts || fabs(fdelta) > s->min_hard_compensation){
944 int ret;
945
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 52 times.
56 if(delta > 0) ret = swr_inject_silence(s, delta / s->out_sample_rate);
946 52 else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
947
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if(ret<0){
948 av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
949 }
950
2/4
✓ Branch 0 taken 127 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
✗ Branch 3 not taken.
127 } else if(s->soft_compensation_duration && s->max_soft_compensation) {
951 127 int duration = s->out_sample_rate * s->soft_compensation_duration;
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 double max_soft_compensation = s->max_soft_compensation / (s->max_soft_compensation < 0 ? -s->in_sample_rate : 1);
953 127 int comp = av_clipf(fdelta, -max_soft_compensation, max_soft_compensation) * duration ;
954 127 av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
955 127 swr_set_compensation(s, comp, duration);
956 }
957 }
958
959 1231 return s->outpts;
960 }
961 }
962