yading@11
|
1 /*
|
yading@11
|
2 * audio resampling with soxr
|
yading@11
|
3 * Copyright (c) 2012 Rob Sykes <robs@users.sourceforge.net>
|
yading@11
|
4 *
|
yading@11
|
5 * This file is part of FFmpeg.
|
yading@11
|
6 *
|
yading@11
|
7 * FFmpeg is free software; you can redistribute it and/or
|
yading@11
|
8 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
9 * License as published by the Free Software Foundation; either
|
yading@11
|
10 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
11 *
|
yading@11
|
12 * FFmpeg is distributed in the hope that it will be useful,
|
yading@11
|
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
15 * Lesser General Public License for more details.
|
yading@11
|
16 *
|
yading@11
|
17 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
18 * License along with FFmpeg; if not, write to the Free Software
|
yading@11
|
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
20 */
|
yading@11
|
21
|
yading@11
|
22 /**
|
yading@11
|
23 * @file
|
yading@11
|
24 * audio resampling with soxr
|
yading@11
|
25 */
|
yading@11
|
26
|
yading@11
|
27 #include "libavutil/log.h"
|
yading@11
|
28 #include "swresample_internal.h"
|
yading@11
|
29
|
yading@11
|
30 #include <soxr.h>
|
yading@11
|
31
|
yading@11
|
32 static struct ResampleContext *create(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
|
yading@11
|
33 double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta, double precision, int cheby){
|
yading@11
|
34 soxr_error_t error;
|
yading@11
|
35
|
yading@11
|
36 soxr_datatype_t type =
|
yading@11
|
37 format == AV_SAMPLE_FMT_S16P? SOXR_INT16_S :
|
yading@11
|
38 format == AV_SAMPLE_FMT_S16 ? SOXR_INT16_I :
|
yading@11
|
39 format == AV_SAMPLE_FMT_S32P? SOXR_INT32_S :
|
yading@11
|
40 format == AV_SAMPLE_FMT_S32 ? SOXR_INT32_I :
|
yading@11
|
41 format == AV_SAMPLE_FMT_FLTP? SOXR_FLOAT32_S :
|
yading@11
|
42 format == AV_SAMPLE_FMT_FLT ? SOXR_FLOAT32_I :
|
yading@11
|
43 format == AV_SAMPLE_FMT_DBLP? SOXR_FLOAT64_S :
|
yading@11
|
44 format == AV_SAMPLE_FMT_DBL ? SOXR_FLOAT64_I : (soxr_datatype_t)-1;
|
yading@11
|
45
|
yading@11
|
46 soxr_io_spec_t io_spec = soxr_io_spec(type, type);
|
yading@11
|
47
|
yading@11
|
48 soxr_quality_spec_t q_spec = soxr_quality_spec((int)((precision-2)/4), (SOXR_HI_PREC_CLOCK|SOXR_ROLLOFF_NONE)*!!cheby);
|
yading@11
|
49 q_spec.precision = linear? 0 : precision;
|
yading@11
|
50 #if !defined SOXR_VERSION /* Deprecated @ March 2013: */
|
yading@11
|
51 q_spec.bw_pc = cutoff? FFMAX(FFMIN(cutoff,.995),.8)*100 : q_spec.bw_pc;
|
yading@11
|
52 #else
|
yading@11
|
53 q_spec.passband_end = cutoff? FFMAX(FFMIN(cutoff,.995),.8) : q_spec.passband_end;
|
yading@11
|
54 #endif
|
yading@11
|
55
|
yading@11
|
56 soxr_delete((soxr_t)c);
|
yading@11
|
57 c = (struct ResampleContext *)
|
yading@11
|
58 soxr_create(in_rate, out_rate, 0, &error, &io_spec, &q_spec, 0);
|
yading@11
|
59 if (!c)
|
yading@11
|
60 av_log(NULL, AV_LOG_ERROR, "soxr_create: %s\n", error);
|
yading@11
|
61 return c;
|
yading@11
|
62 }
|
yading@11
|
63
|
yading@11
|
64 static void destroy(struct ResampleContext * *c){
|
yading@11
|
65 soxr_delete((soxr_t)*c);
|
yading@11
|
66 *c = NULL;
|
yading@11
|
67 }
|
yading@11
|
68
|
yading@11
|
69 static int flush(struct SwrContext *s){
|
yading@11
|
70 soxr_process((soxr_t)s->resample, NULL, 0, NULL, NULL, 0, NULL);
|
yading@11
|
71 return 0;
|
yading@11
|
72 }
|
yading@11
|
73
|
yading@11
|
74 static int process(
|
yading@11
|
75 struct ResampleContext * c, AudioData *dst, int dst_size,
|
yading@11
|
76 AudioData *src, int src_size, int *consumed){
|
yading@11
|
77 size_t idone, odone;
|
yading@11
|
78 soxr_error_t error = soxr_set_error((soxr_t)c, soxr_set_num_channels((soxr_t)c, src->ch_count));
|
yading@11
|
79 error = soxr_process((soxr_t)c, src->ch, (size_t)src_size,
|
yading@11
|
80 &idone, dst->ch, (size_t)dst_size, &odone);
|
yading@11
|
81 *consumed = (int)idone;
|
yading@11
|
82 return error? -1 : odone;
|
yading@11
|
83 }
|
yading@11
|
84
|
yading@11
|
85 static int64_t get_delay(struct SwrContext *s, int64_t base){
|
yading@11
|
86 double delay_s = soxr_delay((soxr_t)s->resample) / s->out_sample_rate;
|
yading@11
|
87 return (int64_t)(delay_s * base + .5);
|
yading@11
|
88 }
|
yading@11
|
89
|
yading@11
|
90 struct Resampler const soxr_resampler={
|
yading@11
|
91 create, destroy, process, flush, NULL /* set_compensation */, get_delay,
|
yading@11
|
92 };
|
yading@11
|
93
|