Coverage Report

Created: 2026-05-23 07:06

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