FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswresample/swresample.c
Date: 2026-09-26 05:01:43
Exec Total Coverage
Lines: 479 634 75.6%
Functions: 23 24 95.8%
Branches: 361 547 66.0%

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 6502 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 6502 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 6502 times.
6502 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 6502 return 0;
45 }
46
47 19 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){
48
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
19 if(!s || s->in_convert) // s needs to be allocated but not initialized
49 ✗ return AVERROR(EINVAL);
50 19 s->channel_map = channel_map;
51 19 return 0;
52 }
53
54 1601 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 1601 struct SwrContext *s = *ps;
59 int ret;
60
61
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1571 times.
1601 if (!s) s = swr_alloc();
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1601 times.
1601 if (!s) return AVERROR(ENOMEM);
63
64 1601 *ps = s;
65
66 1601 s->log_level_offset = log_offset;
67 1601 s->log_ctx = log_ctx;
68
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 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 1601 times.
1601 if ((ret = av_opt_set_int(s, "isr", in_sample_rate, 0)) < 0)
89 ✗ goto fail;
90
91 1601 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 6146 static void set_audiodata_fmt(AudioData *a, enum AVSampleFormat fmt){
99 6146 a->fmt = fmt;
100 6146 a->bps = av_get_bytes_per_sample(fmt);
101 6146 a->planar= av_sample_fmt_is_planar(fmt);
102
2/2
✓ Branch 0 taken 4664 times.
✓ Branch 1 taken 1482 times.
6146 if (a->ch_count == 1)
103 4664 a->planar = 1;
104 6146 }
105
106 34176 static void free_temp(AudioData *a){
107 34176 av_free(a->data);
108 34176 memset(a, 0, sizeof(*a));
109 34176 }
110
111 4272 static void clear_context(SwrContext *s){
112 4272 s->in_buffer_index= 0;
113 4272 s->in_buffer_count= 0;
114 4272 s->resample_in_constraint= 0;
115 4272 memset(s->in.ch, 0, sizeof(s->in.ch));
116 4272 memset(s->out.ch, 0, sizeof(s->out.ch));
117 4272 free_temp(&s->postin);
118 4272 free_temp(&s->midbuf);
119 4272 free_temp(&s->preout);
120 4272 free_temp(&s->in_buffer);
121 4272 free_temp(&s->silence);
122 4272 free_temp(&s->drop_temp);
123 4272 free_temp(&s->dither.noise);
124 4272 free_temp(&s->dither.temp);
125 4272 av_channel_layout_uninit(&s->in_ch_layout);
126 4272 av_channel_layout_uninit(&s->out_ch_layout);
127 4272 av_channel_layout_uninit(&s->used_ch_layout);
128 4272 swri_audio_convert_free(&s-> in_convert);
129 4272 swri_audio_convert_free(&s->out_convert);
130 4272 swri_audio_convert_free(&s->full_convert);
131 4272 swri_rematrix_free(s);
132
133 4272 s->delayed_samples_fixup = 0;
134 4272 s->flushed = 0;
135 4272 }
136
137 2652 av_cold void swr_free(SwrContext **ss){
138 2652 SwrContext *s= *ss;
139
2/2
✓ Branch 0 taken 2614 times.
✓ Branch 1 taken 38 times.
2652 if(s){
140 2614 clear_context(s);
141 2614 av_channel_layout_uninit(&s->user_in_chlayout);
142 2614 av_channel_layout_uninit(&s->user_out_chlayout);
143 2614 av_channel_layout_uninit(&s->user_used_chlayout);
144
145
2/2
✓ Branch 0 taken 1626 times.
✓ Branch 1 taken 988 times.
2614 if (s->resampler)
146 1626 s->resampler->free(&s->resample);
147 }
148
149 2652 av_freep(ss);
150 2652 }
151
152 18 av_cold void swr_close(SwrContext *s){
153 18 clear_context(s);
154 18 }
155
156 1640 av_cold int swr_init(struct SwrContext *s){
157 int ret;
158 char l1[1024], l2[1024];
159
160 1640 clear_context(s);
161
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 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 1640 times.
1640 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 1640 times.
1640 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 1640 times.
1640 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if (s->out_sample_fmt == AV_SAMPLE_FMT_DSD &&
181 ✗ !(s->in_sample_fmt == AV_SAMPLE_FMT_DSD &&
182 ✗ s->in_sample_rate == s->out_sample_rate &&
183 ✗ !(s->flags & SWR_FLAG_RESAMPLE))) {
184 ✗ av_log(s, AV_LOG_ERROR, "Conversion to DSD is not supported\n");
185 ✗ return AVERROR(EINVAL);
186 }
187
188 1640 s->out.ch_count = s-> user_out_chlayout.nb_channels;
189 1640 s-> in.ch_count = s-> user_in_chlayout.nb_channels;
190
191
2/4
✓ Branch 1 taken 1640 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1640 times.
3280 if (swri_check_chlayout(s, &s->user_in_chlayout , "input") ||
192 1640 swri_check_chlayout(s, &s->user_out_chlayout, "output"))
193 ✗ return AVERROR(EINVAL);
194
195 1640 ret = av_channel_layout_copy(&s->in_ch_layout, &s->user_in_chlayout);
196 1640 ret |= av_channel_layout_copy(&s->out_ch_layout, &s->user_out_chlayout);
197 1640 ret |= av_channel_layout_copy(&s->used_ch_layout, &s->user_used_chlayout);
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if (ret < 0)
199 ✗ return ret;
200
201 1640 s->int_sample_fmt= s->user_int_sample_fmt;
202
203 1640 s->dither.method = s->user_dither_method;
204
205
1/2
✓ Branch 0 taken 1640 times.
✗ Branch 1 not taken.
1640 switch(s->engine){
206 #if CONFIG_LIBSOXR
207 case SWR_ENGINE_SOXR: s->resampler = &swri_soxr_resampler; break;
208 #endif
209 1640 case SWR_ENGINE_SWR : s->resampler = &swri_resampler; break;
210 ✗ default:
211 ✗ av_log(s, AV_LOG_ERROR, "Requested resampling engine is unavailable\n");
212 ✗ return AVERROR(EINVAL);
213 }
214
215
2/2
✓ Branch 1 taken 1621 times.
✓ Branch 2 taken 19 times.
1640 if (!av_channel_layout_check(&s->used_ch_layout))
216 1621 av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
217
218
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1631 times.
1640 if (s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels)
219 9 av_channel_layout_uninit(&s->in_ch_layout);
220
221
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1639 times.
1640 if (s->used_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
222 1 av_channel_layout_default(&s->used_ch_layout, s->used_ch_layout.nb_channels);
223
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1613 times.
1640 if (s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) {
224 27 ret = av_channel_layout_copy(&s->in_ch_layout, &s->used_ch_layout);
225
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (ret < 0)
226 ✗ return ret;
227 }
228
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 1621 times.
1640 if (s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
229 19 av_channel_layout_default(&s->out_ch_layout, s->out.ch_count);
230
231 1640 s->rematrix = av_channel_layout_compare(&s->out_ch_layout, &s->in_ch_layout) ||
232
3/4
✓ Branch 0 taken 1612 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 1612 times.
✗ Branch 3 not taken.
3252 s->rematrix_volume!=1.0 ||
233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1610 times.
1612 s->rematrix_custom;
234
235
2/2
✓ Branch 0 taken 999 times.
✓ Branch 1 taken 641 times.
1640 if(s->int_sample_fmt == AV_SAMPLE_FMT_NONE){
236 // DSD to PCM conversion is done in floating point
237
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 994 times.
999 if( s->in_sample_fmt == AV_SAMPLE_FMT_DSD
238
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 && s->out_sample_fmt != AV_SAMPLE_FMT_DSD) {
239 5 s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
240 // 16bit or less to 16bit or less with the same sample rate
241
2/2
✓ Branch 1 taken 579 times.
✓ Branch 2 taken 415 times.
994 } else if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
242
2/2
✓ Branch 1 taken 491 times.
✓ Branch 2 taken 88 times.
579 && av_get_bytes_per_sample(s->out_sample_fmt) <= 2
243
2/2
✓ Branch 0 taken 490 times.
✓ Branch 1 taken 1 times.
491 && s->out_sample_rate==s->in_sample_rate) {
244 490 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
245 // 8 -> 8, 16->8, 8->16bit
246 504 } else if( av_get_bytes_per_sample(s-> in_sample_fmt)
247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 504 times.
504 +av_get_bytes_per_sample(s->out_sample_fmt) <= 3 ) {
248 ✗ s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
249
2/2
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 415 times.
504 }else if( av_get_bytes_per_sample(s-> in_sample_fmt) <= 2
250
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 2 times.
89 && !s->rematrix
251
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 1 times.
87 && s->out_sample_rate==s->in_sample_rate
252
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 && !(s->flags & SWR_FLAG_RESAMPLE)){
253 86 s->int_sample_fmt= AV_SAMPLE_FMT_S16P;
254
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 342 times.
418 }else if( av_get_planar_sample_fmt(s-> in_sample_fmt) == AV_SAMPLE_FMT_S32P
255
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
256
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && !s->rematrix
257
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && s->out_sample_rate == s->in_sample_rate
258
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && !(s->flags & SWR_FLAG_RESAMPLE)
259
1/2
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
42 && s->engine != SWR_ENGINE_SOXR){
260 42 s->int_sample_fmt= AV_SAMPLE_FMT_S32P;
261
2/2
✓ Branch 1 taken 331 times.
✓ Branch 2 taken 45 times.
376 }else if(av_get_bytes_per_sample(s->in_sample_fmt) <= 4){
262 331 s->int_sample_fmt= AV_SAMPLE_FMT_FLTP;
263 }else{
264 45 s->int_sample_fmt= AV_SAMPLE_FMT_DBLP;
265 }
266 }
267 1640 av_log(s, AV_LOG_DEBUG, "Using %s internally between filters\n", av_get_sample_fmt_name(s->int_sample_fmt));
268
269
2/2
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 752 times.
1640 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
270
2/2
✓ Branch 0 taken 701 times.
✓ Branch 1 taken 187 times.
888 &&s->int_sample_fmt != AV_SAMPLE_FMT_S32P
271
1/2
✓ Branch 0 taken 701 times.
✗ Branch 1 not taken.
701 &&s->int_sample_fmt != AV_SAMPLE_FMT_S64P
272
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 500 times.
701 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 &&s->int_sample_fmt != AV_SAMPLE_FMT_DBLP){
274 ✗ 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));
275 ✗ return AVERROR(EINVAL);
276 }
277
278 1640 set_audiodata_fmt(&s-> in, s-> in_sample_fmt);
279 1640 set_audiodata_fmt(&s->out, s->out_sample_fmt);
280
281
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1585 times.
1640 if (s->firstpts_in_samples != AV_NOPTS_VALUE) {
282
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)
283 ✗ s->async = 1;
284
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1 times.
55 if (s->firstpts == AV_NOPTS_VALUE)
285 54 s->firstpts =
286 54 s->outpts = s->firstpts_in_samples * s->out_sample_rate;
287 } else
288 1585 s->firstpts = AV_NOPTS_VALUE;
289
290
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 1586 times.
1640 if (s->async) {
291
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 1 times.
54 if (s->min_compensation >= FLT_MAX/2)
292 53 s->min_compensation = 0.001;
293
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (s->async > 1.0001) {
294 54 s->max_soft_compensation = s->async / (double) s->in_sample_rate;
295 }
296 }
297
298
4/4
✓ Branch 0 taken 953 times.
✓ Branch 1 taken 687 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 952 times.
1640 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
299 688 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);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 688 times.
688 if (!s->resample) {
301 ✗ av_log(s, AV_LOG_ERROR, "Failed to initialize resampler\n");
302 ✗ return AVERROR(ENOMEM);
303 }
304 }else
305 952 s->resampler->free(&s->resample);
306
2/2
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 752 times.
1640 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16P
307
2/2
✓ Branch 0 taken 701 times.
✓ Branch 1 taken 187 times.
888 && s->int_sample_fmt != AV_SAMPLE_FMT_S32P
308
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 500 times.
701 && s->int_sample_fmt != AV_SAMPLE_FMT_FLTP
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 201 times.
201 && s->int_sample_fmt != AV_SAMPLE_FMT_DBLP
310 ✗ && s->resample){
311 ✗ av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16p/s32p/fltp/dblp\n");
312 ✗ ret = AVERROR(EINVAL);
313 ✗ goto fail;
314 }
315
316 #define RSC 1 //FIXME finetune
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if(!s-> in.ch_count)
318 ✗ s-> in.ch_count = s->in_ch_layout.nb_channels;
319
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1640 times.
1640 if (!av_channel_layout_check(&s->used_ch_layout))
320 ✗ av_channel_layout_default(&s->used_ch_layout, s->in.ch_count);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if(!s->out.ch_count)
322 ✗ s->out.ch_count = s->out_ch_layout.nb_channels;
323
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 if(!s-> in.ch_count){
325 ✗ av_assert0(s->in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC);
326 ✗ av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n");
327 ✗ ret = AVERROR(EINVAL);
328 ✗ goto fail;
329 }
330
331 1640 av_channel_layout_describe(&s->out_ch_layout, l2, sizeof(l2));
332 1640 av_channel_layout_describe(&s->in_ch_layout, l1, sizeof(l1));
333
3/4
✓ Branch 0 taken 1639 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1639 times.
1640 if (s->in_ch_layout.order != AV_CHANNEL_ORDER_UNSPEC && s->used_ch_layout.nb_channels != s->in_ch_layout.nb_channels) {
334 ✗ av_log(s, AV_LOG_ERROR, "Input channel layout %s mismatches specified channel count %d\n", l1, s->used_ch_layout.nb_channels);
335 ✗ ret = AVERROR(EINVAL);
336 ✗ goto fail;
337 }
338
339
2/2
✓ Branch 0 taken 1639 times.
✓ Branch 1 taken 1 times.
1640 if (( s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC
340
4/6
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1638 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
1640 || s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC) && s->used_ch_layout.nb_channels != s->out.ch_count && !s->rematrix_custom) {
341 ✗ av_log(s, AV_LOG_ERROR, "Rematrix is needed between %s and %s "
342 "but there is not enough information to do it\n", l1, l2);
343 ✗ ret = AVERROR(EINVAL);
344 ✗ goto fail;
345 }
346
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 av_assert0(s->used_ch_layout.nb_channels);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1640 times.
1640 av_assert0(s->out.ch_count);
349 1640 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;
350
351 1640 s->in_buffer= s->in;
352 1640 s->silence = s->in;
353 1640 s->drop_temp= s->out;
354
355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1640 times.
1640 if ((ret = swri_dither_init(s, s->out_sample_fmt, s->int_sample_fmt)) < 0)
356 ✗ goto fail;
357
358
7/8
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 688 times.
✓ Branch 2 taken 926 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 915 times.
✓ Branch 5 taken 11 times.
✓ Branch 6 taken 915 times.
✗ Branch 7 not taken.
1640 if(!s->resample && !s->rematrix && !s->channel_map && !s->dither.method){
359 915 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
360 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
361 // fall through to the generic path for conversions that have no
362 // direct implementation (e.g. DSD input to non-float output)
363
2/2
✓ Branch 0 taken 914 times.
✓ Branch 1 taken 1 times.
915 if (s->full_convert)
364 914 return 0;
365 }
366
367 726 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
368 s-> in_sample_fmt, s->used_ch_layout.nb_channels, s->channel_map, 0);
369 726 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
370 s->int_sample_fmt, s->out.ch_count, NULL, 0);
371
372
2/4
✓ Branch 0 taken 726 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 726 times.
726 if (!s->in_convert || !s->out_convert) {
373 ✗ av_log(s, AV_LOG_ERROR, "Cannot convert %s sample format to %s sample format\n",
374 ✗ av_get_sample_fmt_name(!s->in_convert ? s->in_sample_fmt : s->int_sample_fmt),
375 ✗ av_get_sample_fmt_name(!s->in_convert ? s->int_sample_fmt : s->out_sample_fmt));
376 ✗ ret = AVERROR(EINVAL);
377 ✗ goto fail;
378 }
379
380 726 s->postin= s->in;
381 726 s->preout= s->out;
382 726 s->midbuf= s->in;
383
384
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 707 times.
726 if(s->channel_map){
385 19 s->postin.ch_count=
386 19 s->midbuf.ch_count= s->used_ch_layout.nb_channels;
387
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if(s->resample)
388 ✗ s->in_buffer.ch_count= s->used_ch_layout.nb_channels;
389 }
390
2/2
✓ Branch 0 taken 354 times.
✓ Branch 1 taken 372 times.
726 if(!s->resample_first){
391 354 s->midbuf.ch_count= s->out.ch_count;
392
2/2
✓ Branch 0 taken 328 times.
✓ Branch 1 taken 26 times.
354 if(s->resample)
393 328 s->in_buffer.ch_count = s->out.ch_count;
394 }
395
396 726 set_audiodata_fmt(&s->postin, s->int_sample_fmt);
397 726 set_audiodata_fmt(&s->midbuf, s->int_sample_fmt);
398 726 set_audiodata_fmt(&s->preout, s->int_sample_fmt);
399
400
2/2
✓ Branch 0 taken 688 times.
✓ Branch 1 taken 38 times.
726 if(s->resample){
401 688 set_audiodata_fmt(&s->in_buffer, s->int_sample_fmt);
402 }
403
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 726 times.
726 av_assert0(!s->preout.count);
405 726 s->dither.noise = s->preout;
406 726 s->dither.temp = s->preout;
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 726 times.
726 if (s->dither.method > SWR_DITHER_NS) {
408 ✗ s->dither.noise.bps = 4;
409 ✗ s->dither.noise.fmt = AV_SAMPLE_FMT_FLTP;
410 ✗ s->dither.noise_scale = 1;
411 }
412
413
3/4
✓ Branch 0 taken 696 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 696 times.
726 if(s->rematrix || s->dither.method) {
414 30 ret = swri_rematrix_init(s);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (ret < 0)
416 ✗ goto fail;
417 }
418
419 726 return 0;
420 ✗ fail:
421 ✗ swr_close(s);
422 ✗ return ret;
423
424 }
425
426 102443 int swri_realloc_audio(AudioData *a, int count){
427 int i, countb;
428 AudioData old;
429
430
2/4
✓ Branch 0 taken 102443 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 102443 times.
102443 if(count < 0 || count > INT_MAX/2/a->bps/a->ch_count)
431 ✗ return AVERROR(EINVAL);
432
433
2/2
✓ Branch 0 taken 98356 times.
✓ Branch 1 taken 4087 times.
102443 if(a->count >= count)
434 98356 return 0;
435
436 4087 count*=2;
437
438 4087 countb= FFALIGN(count*a->bps, ALIGN);
439 4087 old= *a;
440
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4087 times.
4087 av_assert0(a->bps);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4087 times.
4087 av_assert0(a->ch_count);
443
444 4087 a->data = av_calloc(countb, a->ch_count);
445
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4087 times.
4087 if(!a->data)
446 ✗ return AVERROR(ENOMEM);
447
2/2
✓ Branch 0 taken 4609 times.
✓ Branch 1 taken 4087 times.
8696 for(i=0; i<a->ch_count; i++){
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4609 times.
4609 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
449
3/4
✓ Branch 0 taken 1252 times.
✓ Branch 1 taken 3357 times.
✓ Branch 2 taken 1252 times.
✗ Branch 3 not taken.
4609 if(a->count && a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
450 }
451
3/4
✓ Branch 0 taken 1164 times.
✓ Branch 1 taken 2923 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1164 times.
4087 if(a->count && !a->planar) memcpy(a->ch[0], old.ch[0], a->count*a->ch_count*a->bps);
452 4087 av_freep(&old.data);
453 4087 a->count= count;
454
455 4087 return 1;
456 }
457
458 35927 static void copy(AudioData *out, AudioData *in,
459 int count){
460
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35927 times.
35927 av_assert0(out->planar == in->planar);
461
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35927 times.
35927 av_assert0(out->bps == in->bps);
462
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35927 times.
35927 av_assert0(out->ch_count == in->ch_count);
463
1/2
✓ Branch 0 taken 35927 times.
✗ Branch 1 not taken.
35927 if(out->planar){
464 int ch;
465
2/2
✓ Branch 0 taken 56540 times.
✓ Branch 1 taken 35927 times.
92467 for(ch=0; ch<out->ch_count; ch++)
466 56540 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
467 }else
468 ✗ memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
469 35927 }
470
471 556834 static void fill_audiodata(AudioData *out, uint8_t *const in_arg [SWR_CH_MAX])
472 {
473 int i;
474
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 556764 times.
556834 if(!in_arg){
475 70 memset(out->ch, 0, sizeof(out->ch));
476
2/2
✓ Branch 0 taken 403257 times.
✓ Branch 1 taken 153507 times.
556764 }else if(out->planar){
477
2/2
✓ Branch 0 taken 615969 times.
✓ Branch 1 taken 403257 times.
1019226 for(i=0; i<out->ch_count; i++)
478 615969 out->ch[i]= in_arg[i];
479 }else{
480
2/2
✓ Branch 0 taken 360980 times.
✓ Branch 1 taken 153507 times.
514487 for(i=0; i<out->ch_count; i++)
481 360980 out->ch[i]= in_arg[0] + i*out->bps;
482 }
483 556834 }
484
485 137 static void reversefill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
486 int i;
487
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 if(out->planar){
488
2/2
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 137 times.
274 for(i=0; i<out->ch_count; i++)
489 137 in_arg[i]= out->ch[i];
490 }else{
491 ✗ in_arg[0]= out->ch[0];
492 }
493 137 }
494
495 /**
496 *
497 * out may be equal in.
498 */
499 480955 static void buf_set(AudioData *out, AudioData *in, int count){
500 int ch;
501
2/2
✓ Branch 0 taken 469916 times.
✓ Branch 1 taken 11039 times.
480955 if(in->planar){
502
2/2
✓ Branch 0 taken 784217 times.
✓ Branch 1 taken 469916 times.
1254133 for(ch=0; ch<out->ch_count; ch++)
503 784217 out->ch[ch]= in->ch[ch] + count*out->bps;
504 }else{
505
2/2
✓ Branch 0 taken 28702 times.
✓ Branch 1 taken 11039 times.
39741 for(ch=out->ch_count-1; ch>=0; ch--)
506 28702 out->ch[ch]= in->ch[0] + (ch + count*out->ch_count) * out->bps;
507 }
508 480955 }
509
510 /**
511 *
512 * @return number of samples output per channel
513 */
514 19868 static int resample(SwrContext *s, AudioData *out_param, int out_count,
515 const AudioData * in_param, int in_count){
516 AudioData in, out, tmp;
517 19868 int ret_sum=0;
518 19868 int border=0;
519
1/2
✓ Branch 0 taken 19868 times.
✗ Branch 1 not taken.
19868 int padless = ARCH_X86 && s->engine == SWR_ENGINE_SWR ? 7 : 0;
520
521 av_assert1(s->in_buffer.ch_count == in_param->ch_count);
522 av_assert1(s->in_buffer.planar == in_param->planar);
523 av_assert1(s->in_buffer.fmt == in_param->fmt);
524
525 19868 tmp=out=*out_param;
526 19868 in = *in_param;
527
528 19868 border = s->resampler->invert_initial_buffer(s->resample, &s->in_buffer,
529 &in, in_count, &s->in_buffer_index, &s->in_buffer_count);
530
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 19779 times.
19868 if (border == INT_MAX) {
531 89 return 0;
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19779 times.
19779 } else if (border < 0) {
533 ✗ return border;
534
2/2
✓ Branch 0 taken 686 times.
✓ Branch 1 taken 19093 times.
19779 } else if (border) {
535 686 buf_set(&in, &in, border);
536 686 in_count -= border;
537 686 s->resample_in_constraint = 0;
538 }
539
540 35892 do{
541 int ret, size, consumed;
542
4/4
✓ Branch 0 taken 47461 times.
✓ Branch 1 taken 8210 times.
✓ Branch 2 taken 47455 times.
✓ Branch 3 taken 6 times.
55671 if(!s->resample_in_constraint && s->in_buffer_count){
543 47455 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
544 47455 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
545 47455 out_count -= ret;
546 47455 ret_sum += ret;
547 47455 buf_set(&out, &out, ret);
548 47455 s->in_buffer_count -= consumed;
549 47455 s->in_buffer_index += consumed;
550
551
2/2
✓ Branch 0 taken 19774 times.
✓ Branch 1 taken 27681 times.
47455 if(!in_count)
552 19774 break;
553
2/2
✓ Branch 0 taken 18054 times.
✓ Branch 1 taken 9627 times.
27681 if(s->in_buffer_count <= border){
554 18054 buf_set(&in, &in, -s->in_buffer_count);
555 18054 in_count += s->in_buffer_count;
556 18054 s->in_buffer_count=0;
557 18054 s->in_buffer_index=0;
558 18054 border = 0;
559 }
560 }
561
562
6/6
✓ Branch 0 taken 35893 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 35892 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 18098 times.
✓ Branch 5 taken 17798 times.
35897 if((s->flushed || in_count > padless) && !s->in_buffer_count){
563 18098 s->in_buffer_index=0;
564 18098 ret= s->resampler->multiple_resample(s->resample, &out, out_count, &in, FFMAX(in_count-padless, 0), &consumed);
565 18098 out_count -= ret;
566 18098 ret_sum += ret;
567 18098 buf_set(&out, &out, ret);
568 18098 in_count -= consumed;
569 18098 buf_set(&in, &in, consumed);
570 }
571
572 //TODO is this check sane considering the advanced copy avoidance below
573 35897 size= s->in_buffer_index + s->in_buffer_count + in_count;
574
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 35198 times.
35897 if( size > s->in_buffer.count
575
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 699 times.
699 && s->in_buffer_count + in_count <= s->in_buffer_index){
576 ✗ buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
577 ✗ copy(&s->in_buffer, &tmp, s->in_buffer_count);
578 ✗ s->in_buffer_index=0;
579 }else
580
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35897 times.
35897 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
581 ✗ return ret;
582
583
2/2
✓ Branch 0 taken 35892 times.
✓ Branch 1 taken 5 times.
35897 if(in_count){
584 35892 int count= in_count;
585
6/6
✓ Branch 0 taken 17798 times.
✓ Branch 1 taken 18094 times.
✓ Branch 2 taken 17487 times.
✓ Branch 3 taken 311 times.
✓ Branch 4 taken 17467 times.
✓ Branch 5 taken 20 times.
35892 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
586
587 35892 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
588 35892 copy(&tmp, &in, /*in_*/count);
589 35892 s->in_buffer_count += count;
590 35892 in_count -= count;
591 35892 border += count;
592 35892 buf_set(&in, &in, count);
593 35892 s->resample_in_constraint= 0;
594
3/4
✓ Branch 0 taken 18094 times.
✓ Branch 1 taken 17798 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18094 times.
35892 if(s->in_buffer_count != count || in_count)
595 35892 continue;
596
1/2
✓ Branch 0 taken 18094 times.
✗ Branch 1 not taken.
18094 if (padless) {
597 18094 padless = 0;
598 18094 continue;
599 }
600 }
601 5 break;
602 }while(1);
603
604 19779 s->resample_in_constraint= !!out_count;
605
606 19779 return ret_sum;
607 }
608
609 279033 static int swr_convert_internal(struct SwrContext *s, AudioData *out, int out_count,
610 AudioData *in , int in_count){
611 AudioData *postin, *midbuf, *preout;
612 int ret/*, in_max*/;
613 AudioData preout_tmp, midbuf_tmp;
614
615
2/2
✓ Branch 0 taken 257387 times.
✓ Branch 1 taken 21646 times.
279033 if(s->full_convert){
616
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 257387 times.
257387 av_assert0(!s->resample);
617 257387 swri_audio_convert(s->full_convert, out, in, in_count);
618 257387 return out_count;
619 }
620
621 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
622 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
623
624
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21646 times.
21646 if((ret=swri_realloc_audio(&s->postin, in_count))<0)
625 ✗ return ret;
626
2/2
✓ Branch 0 taken 14858 times.
✓ Branch 1 taken 6788 times.
21646 if(s->resample_first){
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14858 times.
14858 av_assert0(s->midbuf.ch_count == s->used_ch_layout.nb_channels);
628
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14858 times.
14858 if((ret=swri_realloc_audio(&s->midbuf, out_count))<0)
629 ✗ return ret;
630 }else{
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6788 times.
6788 av_assert0(s->midbuf.ch_count == s->out.ch_count);
632
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6788 times.
6788 if((ret=swri_realloc_audio(&s->midbuf, in_count))<0)
633 ✗ return ret;
634 }
635
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 21646 times.
21646 if((ret=swri_realloc_audio(&s->preout, out_count))<0)
636 ✗ return ret;
637
638 21646 postin= &s->postin;
639
640 21646 midbuf_tmp= s->midbuf;
641 21646 midbuf= &midbuf_tmp;
642 21646 preout_tmp= s->preout;
643 21646 preout= &preout_tmp;
644
645
5/6
✓ Branch 0 taken 15473 times.
✓ Branch 1 taken 6173 times.
✓ Branch 2 taken 15473 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15257 times.
✓ Branch 5 taken 216 times.
21646 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar && !s->channel_map)
646 15257 postin= in;
647
648
4/4
✓ Branch 0 taken 14858 times.
✓ Branch 1 taken 6788 times.
✓ Branch 2 taken 6350 times.
✓ Branch 3 taken 15296 times.
21646 if(s->resample_first ? !s->resample : !s->rematrix)
649 6350 midbuf= postin;
650
651
4/4
✓ Branch 0 taken 14858 times.
✓ Branch 1 taken 6788 times.
✓ Branch 2 taken 15962 times.
✓ Branch 3 taken 5684 times.
21646 if(s->resample_first ? !s->rematrix : !s->resample)
652 15962 preout= midbuf;
653
654
3/4
✓ Branch 0 taken 14295 times.
✓ Branch 1 taken 7351 times.
✓ Branch 2 taken 14295 times.
✗ Branch 3 not taken.
21646 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar
655
3/4
✓ Branch 0 taken 1044 times.
✓ Branch 1 taken 13251 times.
✓ Branch 2 taken 1044 times.
✗ Branch 3 not taken.
14295 && !(s->out_sample_fmt==AV_SAMPLE_FMT_S32P && (s->dither.output_sample_bits&31))){
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14295 times.
14295 if(preout==in){
657 ✗ out_count= FFMIN(out_count, in_count); //TODO check at the end if this is needed or redundant
658 ✗ av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
659 ✗ copy(out, in, out_count);
660 ✗ return out_count;
661 }
662
2/2
✓ Branch 0 taken 216 times.
✓ Branch 1 taken 14079 times.
14295 else if(preout==postin) preout= midbuf= postin= out;
663
2/2
✓ Branch 0 taken 13036 times.
✓ Branch 1 taken 1043 times.
14079 else if(preout==midbuf) preout= midbuf= out;
664 1043 else preout= out;
665 }
666
667
2/2
✓ Branch 0 taken 6389 times.
✓ Branch 1 taken 15257 times.
21646 if(in != postin){
668 6389 swri_audio_convert(s->in_convert, postin, in, in_count);
669 }
670
671
2/2
✓ Branch 0 taken 14858 times.
✓ Branch 1 taken 6788 times.
21646 if(s->resample_first){
672
2/2
✓ Branch 0 taken 14556 times.
✓ Branch 1 taken 302 times.
14858 if(postin != midbuf)
673
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 14556 times.
14556 if ((out_count = resample(s, midbuf, out_count, postin, in_count)) < 0)
674 ✗ return out_count;
675
2/2
✓ Branch 0 taken 372 times.
✓ Branch 1 taken 14486 times.
14858 if(midbuf != preout)
676 372 swri_rematrix(s, preout, midbuf, out_count, preout==out);
677 }else{
678
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 6048 times.
6788 if(postin != midbuf)
679 740 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
680
2/2
✓ Branch 0 taken 5312 times.
✓ Branch 1 taken 1476 times.
6788 if(midbuf != preout)
681
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5312 times.
5312 if ((out_count = resample(s, preout, out_count, midbuf, in_count)) < 0)
682 ✗ return out_count;
683 }
684
685
4/4
✓ Branch 0 taken 7351 times.
✓ Branch 1 taken 14295 times.
✓ Branch 2 taken 7020 times.
✓ Branch 3 taken 331 times.
21646 if(preout != out && out_count){
686 7020 AudioData *conv_src = preout;
687
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7020 times.
7020 if(s->dither.method){
688 int ch;
689 ✗ int dither_count= FFMAX(out_count, 1<<16);
690
691 ✗ if (preout == in) {
692 ✗ conv_src = &s->dither.temp;
693 ✗ if((ret=swri_realloc_audio(&s->dither.temp, dither_count))<0)
694 ✗ return ret;
695 }
696
697 ✗ if((ret=swri_realloc_audio(&s->dither.noise, dither_count))<0)
698 ✗ return ret;
699 ✗ if(ret)
700 ✗ for(ch=0; ch<s->dither.noise.ch_count; ch++)
701 ✗ if((ret=swri_get_dither(s, s->dither.noise.ch[ch], s->dither.noise.count, (12345678913579ULL*ch + 3141592) % 2718281828U, s->dither.noise.fmt))<0)
702 ✗ return ret;
703 ✗ av_assert0(s->dither.noise.ch_count == preout->ch_count);
704
705 ✗ if(s->dither.noise_pos + out_count > s->dither.noise.count)
706 ✗ s->dither.noise_pos = 0;
707
708 ✗ if (s->dither.method < SWR_DITHER_NS){
709 ✗ if (s->mix_2_1_simd) {
710 ✗ int len1= out_count&~15;
711 ✗ int off = len1 * preout->bps;
712
713 ✗ if(len1)
714 ✗ for(ch=0; ch<preout->ch_count; ch++)
715 ✗ 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);
716 ✗ if(out_count != len1)
717 ✗ for(ch=0; ch<preout->ch_count; ch++)
718 ✗ 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);
719 } else {
720 ✗ for(ch=0; ch<preout->ch_count; ch++)
721 ✗ 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);
722 }
723 } else {
724 ✗ switch(s->int_sample_fmt) {
725 ✗ case AV_SAMPLE_FMT_S16P :swri_noise_shaping_int16(s, conv_src, preout, &s->dither.noise, out_count); break;
726 ✗ case AV_SAMPLE_FMT_S32P :swri_noise_shaping_int32(s, conv_src, preout, &s->dither.noise, out_count); break;
727 ✗ case AV_SAMPLE_FMT_FLTP :swri_noise_shaping_float(s, conv_src, preout, &s->dither.noise, out_count); break;
728 ✗ case AV_SAMPLE_FMT_DBLP :swri_noise_shaping_double(s,conv_src, preout, &s->dither.noise, out_count); break;
729 }
730 }
731 ✗ s->dither.noise_pos += out_count;
732 }
733 //FIXME packed doesn't need more than 1 chan here!
734 7020 swri_audio_convert(s->out_convert, out, conv_src, out_count);
735 }
736 21646 return out_count;
737 }
738
739 315955 int swr_is_initialized(struct SwrContext *s) {
740 315955 return !!s->in_buffer.ch_count;
741 }
742
743 279814 int attribute_align_arg swr_convert(struct SwrContext *s,
744 uint8_t * const *out_arg, int out_count,
745 const uint8_t * const *in_arg, int in_count)
746 {
747 279814 AudioData * in= &s->in;
748 279814 AudioData *out= &s->out;
749
750
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 279814 times.
279814 if (!swr_is_initialized(s)) {
751 ✗ av_log(s, AV_LOG_ERROR, "Context has not been initialized\n");
752 ✗ return AVERROR(EINVAL);
753 }
754 #if defined(ASSERT_LEVEL) && ASSERT_LEVEL >1
755 int max_output = swr_get_out_samples(s, in_count);
756 #endif
757
758
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 279762 times.
279866 while(s->drop_output > 0){
759 int ret;
760 uint8_t *tmp_arg[SWR_CH_MAX];
761 #define MAX_DROP_STEP 16384
762
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)
763 52 return ret;
764
765 104 reversefill_audiodata(&s->drop_temp, tmp_arg);
766 104 s->drop_output *= -1; //FIXME find a less hackish solution
767
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
768 104 s->drop_output *= -1;
769 104 in_count = 0;
770
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 52 times.
104 if(ret>0) {
771 52 s->drop_output -= ret;
772
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)
773 ✗ return 0;
774 52 continue;
775 }
776
777
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 av_assert0(s->drop_output);
778 52 return 0;
779 }
780
781
2/2
✓ Branch 0 taken 1995 times.
✓ Branch 1 taken 277767 times.
279762 if(!in_arg){
782
2/2
✓ Branch 0 taken 1300 times.
✓ Branch 1 taken 695 times.
1995 if(s->resample){
783
2/2
✓ Branch 0 taken 661 times.
✓ Branch 1 taken 639 times.
1300 if (!s->flushed)
784 661 s->resampler->flush(s);
785 1300 s->resample_in_constraint = 0;
786 1300 s->flushed = 1;
787
1/2
✓ Branch 0 taken 695 times.
✗ Branch 1 not taken.
695 }else if(!s->in_buffer_count){
788 695 return 0;
789 }
790 }else
791 277767 fill_audiodata(in , (void*)in_arg);
792
793 279067 fill_audiodata(out, out_arg);
794
795
2/2
✓ Branch 0 taken 19868 times.
✓ Branch 1 taken 259199 times.
279067 if(s->resample){
796 19868 int ret = swr_convert_internal(s, out, out_count, in, in_count);
797
4/4
✓ Branch 0 taken 19122 times.
✓ Branch 1 taken 746 times.
✓ Branch 2 taken 19070 times.
✓ Branch 3 taken 52 times.
19868 if(ret>0 && !s->drop_output)
798 19070 s->outpts += ret * (int64_t)s->in_sample_rate;
799
800 av_assert2(max_output < 0 || ret <= max_output);
801
802 19868 return ret;
803 }else{
804 259199 AudioData tmp= *in;
805 259199 int ret2=0;
806 int ret, size;
807 259199 size = FFMIN(out_count, s->in_buffer_count);
808
2/2
✓ Branch 0 taken 125 times.
✓ Branch 1 taken 259074 times.
259199 if(size){
809 125 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
810 125 ret= swr_convert_internal(s, out, size, &tmp, size);
811
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125 times.
125 if(ret<0)
812 ✗ return ret;
813 125 ret2= ret;
814 125 s->in_buffer_count -= ret;
815 125 s->in_buffer_index += ret;
816 125 buf_set(out, out, ret);
817 125 out_count -= ret;
818
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 121 times.
125 if(!s->in_buffer_count)
819 4 s->in_buffer_index = 0;
820 }
821
822
2/2
✓ Branch 0 taken 259074 times.
✓ Branch 1 taken 125 times.
259199 if(in_count){
823 259074 size= s->in_buffer_index + s->in_buffer_count + in_count - out_count;
824
825
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 259039 times.
259074 if(in_count > out_count) { //FIXME move after swr_convert_internal
826
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 30 times.
35 if( size > s->in_buffer.count
827
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 && s->in_buffer_count + in_count - out_count <= s->in_buffer_index){
828 ✗ buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
829 ✗ copy(&s->in_buffer, &tmp, s->in_buffer_count);
830 ✗ s->in_buffer_index=0;
831 }else
832
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 if((ret=swri_realloc_audio(&s->in_buffer, size)) < 0)
833 ✗ return ret;
834 }
835
836
2/2
✓ Branch 0 taken 259040 times.
✓ Branch 1 taken 34 times.
259074 if(out_count){
837 259040 size = FFMIN(in_count, out_count);
838 259040 ret= swr_convert_internal(s, out, size, in, size);
839
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 259040 times.
259040 if(ret<0)
840 ✗ return ret;
841 259040 buf_set(in, in, ret);
842 259040 in_count -= ret;
843 259040 ret2 += ret;
844 }
845
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 259039 times.
259074 if(in_count){
846 35 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
847 35 copy(&tmp, in, in_count);
848 35 s->in_buffer_count += in_count;
849 }
850 }
851
3/4
✓ Branch 0 taken 259164 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 259164 times.
✗ Branch 3 not taken.
259199 if(ret2>0 && !s->drop_output)
852 259164 s->outpts += ret2 * (int64_t)s->in_sample_rate;
853 av_assert2(max_output < 0 || ret2 < 0 || ret2 <= max_output);
854 259199 return ret2;
855 }
856 }
857
858 52 int swr_drop_output(struct SwrContext *s, int count){
859 const uint8_t *tmp_arg[SWR_CH_MAX];
860 52 s->drop_output += count;
861
862
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if(s->drop_output <= 0)
863 ✗ return 0;
864
865 52 av_log(s, AV_LOG_VERBOSE, "discarding %d audio samples\n", count);
866 52 return swr_convert(s, NULL, s->drop_output, tmp_arg, 0);
867 }
868
869 33 int swr_inject_silence(struct SwrContext *s, int count){
870 int ret, i;
871 uint8_t *tmp_arg[SWR_CH_MAX];
872
873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if(count <= 0)
874 ✗ return 0;
875
876 #define MAX_SILENCE_STEP 16384
877
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 33 times.
62 while (count > MAX_SILENCE_STEP) {
878
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
29 if ((ret = swr_inject_silence(s, MAX_SILENCE_STEP)) < 0)
879 ✗ return ret;
880 29 count -= MAX_SILENCE_STEP;
881 }
882
883
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
33 if((ret=swri_realloc_audio(&s->silence, count))<0)
884 ✗ return ret;
885
886 {
887
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
66 int fill = s->silence.fmt == AV_SAMPLE_FMT_DSD ? 0x69 :
888
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 s->silence.bps == 1 ? 0x80 : 0;
889
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++) {
890 33 memset(s->silence.ch[i], fill, count*s->silence.bps);
891 } else
892 ✗ memset(s->silence.ch[0], fill, count*s->silence.bps*s->silence.ch_count);
893 }
894
895 33 reversefill_audiodata(&s->silence, tmp_arg);
896 33 av_log(s, AV_LOG_VERBOSE, "adding %d audio samples of silence\n", count);
897 33 ret = swr_convert(s, NULL, 0, (const uint8_t**)tmp_arg, count);
898 33 return ret;
899 }
900
901 532846 int64_t swr_get_delay(struct SwrContext *s, int64_t base){
902
3/4
✓ Branch 0 taken 532846 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17702 times.
✓ Branch 3 taken 515144 times.
532846 if (s->resampler && s->resample){
903 17702 return s->resampler->get_delay(s, base);
904 }else{
905 515144 return (s->in_buffer_count*base + (s->in_sample_rate>>1))/ s->in_sample_rate;
906 }
907 }
908
909 ✗ int swr_get_out_samples(struct SwrContext *s, int in_samples)
910 {
911 int64_t out_samples;
912
913 ✗ if (in_samples < 0)
914 ✗ return AVERROR(EINVAL);
915
916 ✗ if (s->resampler && s->resample) {
917 ✗ if (!s->resampler->get_out_samples)
918 ✗ return AVERROR(ENOSYS);
919 ✗ out_samples = s->resampler->get_out_samples(s, in_samples);
920 } else {
921 ✗ out_samples = s->in_buffer_count + in_samples;
922 ✗ av_assert0(s->out_sample_rate == s->in_sample_rate);
923 }
924
925 ✗ if (out_samples > INT_MAX)
926 ✗ return AVERROR(EINVAL);
927
928 ✗ return out_samples;
929 }
930
931 127 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance){
932 int ret;
933
934
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)
935 ✗ return AVERROR(EINVAL);
936
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)
937 ✗ return AVERROR(EINVAL);
938
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 126 times.
127 if (!s->resample) {
939 1 s->flags |= SWR_FLAG_RESAMPLE;
940 1 ret = swr_init(s);
941
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (ret < 0)
942 ✗ return ret;
943 }
944
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 127 times.
127 if (!s->resampler->set_compensation){
945 ✗ return AVERROR(EINVAL);
946 }else{
947 127 return s->resampler->set_compensation(s->resample, sample_delta, compensation_distance);
948 }
949 }
950
951 268527 int64_t swr_next_pts(struct SwrContext *s, int64_t pts){
952
2/2
✓ Branch 0 taken 2104 times.
✓ Branch 1 taken 266423 times.
268527 if(pts == INT64_MIN)
953 2104 return s->outpts;
954
955
2/2
✓ Branch 0 taken 1516 times.
✓ Branch 1 taken 264907 times.
266423 if (s->firstpts == AV_NOPTS_VALUE)
956 1516 s->outpts = s->firstpts = pts;
957
958
2/2
✓ Branch 0 taken 265192 times.
✓ Branch 1 taken 1231 times.
266423 if(s->min_compensation >= FLT_MAX) {
959 265192 return (s->outpts = pts - swr_get_delay(s, s->in_sample_rate * (int64_t)s->out_sample_rate));
960 } else {
961 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;
962 1231 double fdelta = delta /(double)(s->in_sample_rate * (int64_t)s->out_sample_rate);
963
964
2/2
✓ Branch 0 taken 183 times.
✓ Branch 1 taken 1048 times.
1231 if(fabs(fdelta) > s->min_compensation) {
965
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){
966 int ret;
967
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);
968 52 else ret = swr_drop_output (s, -delta / s-> in_sample_rate);
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if(ret<0){
970 ✗ av_log(s, AV_LOG_ERROR, "Failed to compensate for timestamp delta of %f\n", fdelta);
971 }
972
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) {
973 127 int duration = s->out_sample_rate * s->soft_compensation_duration;
974
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);
975 127 int comp = av_clipf(fdelta, -max_soft_compensation, max_soft_compensation) * duration ;
976 127 av_log(s, AV_LOG_VERBOSE, "compensating audio timestamp drift:%f compensation:%d in:%d\n", fdelta, comp, duration);
977 127 swr_set_compensation(s, comp, duration);
978 }
979 }
980
981 1231 return s->outpts;
982 }
983 }
984