2 * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
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.
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.
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
21 #include "libavutil/opt.h"
22 #include "swresample_internal.h"
23 #include "audioconvert.h"
24 #include "libavutil/avassert.h"
25 #include "libavutil/audioconvert.h"
28 #define C15DB 1.189207115
30 #define C_15DB 0.840896415
31 #define C_30DB M_SQRT1_2
32 #define C_45DB 0.594603558
36 //TODO split options array out?
37 #define OFFSET(x) offsetof(SwrContext,x)
38 static const AVOption options[]={
39 {"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
40 {"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 1, SWR_CH_MAX, 0},
41 {"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0},
42 {"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
43 {"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0},
44 //{"ip" , "input planar" , OFFSET( in.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
45 //{"op" , "output planar" , OFFSET(out.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0},
46 {"isf", "input sample format", OFFSET( in_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
47 {"osf", "output sample format", OFFSET(out_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_S16}, 0, AV_SAMPLE_FMT_NB-1+256, 0},
48 {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0},
49 {"icl", "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
50 {"ocl", "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"},
51 {"clev", "center mix level" , OFFSET(clev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
52 {"slev", "sourround mix level" , OFFSET(slev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0},
53 {"rmvol", "rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0},
54 {"flags", NULL , OFFSET(flags) , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"},
55 {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"},
60 static const char* context_to_name(void* ptr) {
64 static const AVClass av_class = {
65 .class_name = "SwrContext",
66 .item_name = context_to_name,
68 .version = LIBAVUTIL_VERSION_INT,
69 .log_level_offset_offset = OFFSET(log_level_offset),
70 .parent_log_context_offset = OFFSET(log_ctx),
73 static int resample(SwrContext *s, AudioData *out_param, int out_count,
74 const AudioData * in_param, int in_count);
76 struct SwrContext *swr_alloc(void){
77 SwrContext *s= av_mallocz(sizeof(SwrContext));
79 s->av_class= &av_class;
80 av_opt_set_defaults(s);
85 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
86 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
87 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
88 const int *channel_map, int log_offset, void *log_ctx){
89 if(!s) s= swr_alloc();
92 s->log_level_offset= log_offset;
95 av_opt_set_int(s, "ocl", out_ch_layout, 0);
96 av_opt_set_int(s, "osf", out_sample_fmt, 0);
97 av_opt_set_int(s, "osr", out_sample_rate, 0);
98 av_opt_set_int(s, "icl", in_ch_layout, 0);
99 av_opt_set_int(s, "isf", in_sample_fmt, 0);
100 av_opt_set_int(s, "isr", in_sample_rate, 0);
102 s->channel_map = channel_map;
103 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
104 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
105 s->int_sample_fmt = AV_SAMPLE_FMT_S16;
111 static void free_temp(AudioData *a){
113 memset(a, 0, sizeof(*a));
116 void swr_free(SwrContext **ss){
119 free_temp(&s->postin);
120 free_temp(&s->midbuf);
121 free_temp(&s->preout);
122 free_temp(&s->in_buffer);
123 swri_audio_convert_free(&s-> in_convert);
124 swri_audio_convert_free(&s->out_convert);
125 swri_audio_convert_free(&s->full_convert);
126 swri_resample_free(&s->resample);
132 int swr_init(struct SwrContext *s){
133 s->in_buffer_index= 0;
134 s->in_buffer_count= 0;
135 s->resample_in_constraint= 0;
136 free_temp(&s->postin);
137 free_temp(&s->midbuf);
138 free_temp(&s->preout);
139 free_temp(&s->in_buffer);
140 swri_audio_convert_free(&s-> in_convert);
141 swri_audio_convert_free(&s->out_convert);
142 swri_audio_convert_free(&s->full_convert);
144 s-> in.planar= s-> in_sample_fmt >= 0x100;
145 s->out.planar= s->out_sample_fmt >= 0x100;
146 s-> in_sample_fmt &= 0xFF;
147 s->out_sample_fmt &= 0xFF;
149 if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){
150 av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->in_sample_fmt));
151 return AVERROR(EINVAL);
153 if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){
154 av_log(s, AV_LOG_ERROR, "Requested sample format %s is invalid\n", av_get_sample_fmt_name(s->out_sample_fmt));
155 return AVERROR(EINVAL);
158 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16
159 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){
160 av_log(s, AV_LOG_ERROR, "Requested sample format %s is not supported internally, only float & S16 is supported\n", av_get_sample_fmt_name(s->int_sample_fmt));
161 return AVERROR(EINVAL);
164 //FIXME should we allow/support using FLT on material that doesnt need it ?
165 if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){
166 s->int_sample_fmt= AV_SAMPLE_FMT_S16;
168 s->int_sample_fmt= AV_SAMPLE_FMT_FLT;
171 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){
172 s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8);
174 swri_resample_free(&s->resample);
175 if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){
176 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME
180 if(!s->used_ch_count)
181 s->used_ch_count= s->in.ch_count;
183 if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){
184 av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n");
188 if(!s-> in_ch_layout)
189 s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count);
190 if(!s->out_ch_layout)
191 s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count);
193 s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0;
195 #define RSC 1 //FIXME finetune
197 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout);
198 if(!s->used_ch_count)
199 s->used_ch_count= s->in.ch_count;
201 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout);
203 av_assert0(s-> in.ch_count);
204 av_assert0(s->used_ch_count);
205 av_assert0(s->out.ch_count);
206 s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0;
208 s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt);
209 s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt);
210 s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt);
212 if(!s->resample && !s->rematrix && !s->channel_map){
213 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt,
214 s-> in_sample_fmt, s-> in.ch_count, NULL, 0);
218 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt,
219 s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0);
220 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt,
221 s->int_sample_fmt, s->out.ch_count, NULL, 0);
231 s->in_buffer.ch_count= s->used_ch_count;
233 if(!s->resample_first){
234 s->midbuf.ch_count= s->out.ch_count;
235 s->in_buffer.ch_count = s->out.ch_count;
238 s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps;
239 s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar = 1;
242 if(s->rematrix && swri_rematrix_init(s)<0)
248 static int realloc_audio(AudioData *a, int count){
252 if(a->count >= count)
257 countb= FFALIGN(count*a->bps, 32);
260 av_assert0(a->planar);
262 av_assert0(a->ch_count);
264 a->data= av_malloc(countb*a->ch_count);
266 return AVERROR(ENOMEM);
267 for(i=0; i<a->ch_count; i++){
268 a->ch[i]= a->data + i*(a->planar ? countb : a->bps);
269 if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps);
277 static void copy(AudioData *out, AudioData *in,
279 av_assert0(out->planar == in->planar);
280 av_assert0(out->bps == in->bps);
281 av_assert0(out->ch_count == in->ch_count);
284 for(ch=0; ch<out->ch_count; ch++)
285 memcpy(out->ch[ch], in->ch[ch], count*out->bps);
287 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps);
290 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){
293 for(i=0; i<out->ch_count; i++)
294 out->ch[i]= in_arg[i];
296 for(i=0; i<out->ch_count; i++)
297 out->ch[i]= in_arg[0] + i*out->bps;
301 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count,
302 const uint8_t *in_arg [SWR_CH_MAX], int in_count){
303 AudioData *postin, *midbuf, *preout;
305 AudioData * in= &s->in;
306 AudioData *out= &s->out;
307 AudioData preout_tmp, midbuf_tmp;
310 if(in_count > out_count)
312 out_count = in_count;
316 if(s->in_buffer_count){
317 AudioData *a= &s->in_buffer;
319 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0)
321 av_assert0(a->planar);
322 for(i=0; i<a->ch_count; i++){
323 for(j=0; j<s->in_buffer_count; j++){
324 memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps,
325 a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps);
328 s->in_buffer_count += (s->in_buffer_count+1)/2;
329 s->resample_in_constraint = 0;
334 fill_audiodata(in , (void*)in_arg);
335 fill_audiodata(out, out_arg);
338 av_assert0(!s->resample);
339 swri_audio_convert(s->full_convert, out, in, in_count);
343 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps;
344 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count);
346 if((ret=realloc_audio(&s->postin, in_count))<0)
348 if(s->resample_first){
349 av_assert0(s->midbuf.ch_count == s->used_ch_count);
350 if((ret=realloc_audio(&s->midbuf, out_count))<0)
353 av_assert0(s->midbuf.ch_count == s->out.ch_count);
354 if((ret=realloc_audio(&s->midbuf, in_count))<0)
357 if((ret=realloc_audio(&s->preout, out_count))<0)
362 midbuf_tmp= s->midbuf;
364 preout_tmp= s->preout;
367 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar)
370 if(s->resample_first ? !s->resample : !s->rematrix)
373 if(s->resample_first ? !s->rematrix : !s->resample)
376 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){
378 out_count= FFMIN(out_count, in_count); //TODO check at teh end if this is needed or redundant
379 av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though
380 copy(out, in, out_count);
383 else if(preout==postin) preout= midbuf= postin= out;
384 else if(preout==midbuf) preout= midbuf= out;
389 swri_audio_convert(s->in_convert, postin, in, in_count);
392 if(s->resample_first){
394 out_count= resample(s, midbuf, out_count, postin, in_count);
396 swri_rematrix(s, preout, midbuf, out_count, preout==out);
399 swri_rematrix(s, midbuf, postin, in_count, midbuf==out);
401 out_count= resample(s, preout, out_count, midbuf, in_count);
405 //FIXME packed doesnt need more than 1 chan here!
406 swri_audio_convert(s->out_convert, out, preout, out_count);
409 s->in_buffer_count = 0;
415 * out may be equal in.
417 static void buf_set(AudioData *out, AudioData *in, int count){
420 for(ch=0; ch<out->ch_count; ch++)
421 out->ch[ch]= in->ch[ch] + count*out->bps;
423 out->ch[0]= in->ch[0] + count*out->ch_count*out->bps;
428 * @return number of samples output per channel
430 static int resample(SwrContext *s, AudioData *out_param, int out_count,
431 const AudioData * in_param, int in_count){
432 AudioData in, out, tmp;
440 int ret, size, consumed;
441 if(!s->resample_in_constraint && s->in_buffer_count){
442 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
443 ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed);
446 buf_set(&out, &out, ret);
447 s->in_buffer_count -= consumed;
448 s->in_buffer_index += consumed;
452 if(s->in_buffer_count <= border){
453 buf_set(&in, &in, -s->in_buffer_count);
454 in_count += s->in_buffer_count;
455 s->in_buffer_count=0;
456 s->in_buffer_index=0;
461 if(in_count && !s->in_buffer_count){
462 s->in_buffer_index=0;
463 ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed);
466 buf_set(&out, &out, ret);
467 in_count -= consumed;
468 buf_set(&in, &in, consumed);
471 //TODO is this check sane considering the advanced copy avoidance below
472 size= s->in_buffer_index + s->in_buffer_count + in_count;
473 if( size > s->in_buffer.count
474 && s->in_buffer_count + in_count <= s->in_buffer_index){
475 buf_set(&tmp, &s->in_buffer, s->in_buffer_index);
476 copy(&s->in_buffer, &tmp, s->in_buffer_count);
477 s->in_buffer_index=0;
479 if((ret=realloc_audio(&s->in_buffer, size)) < 0)
484 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2;
486 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count);
487 copy(&tmp, &in, /*in_*/count);
488 s->in_buffer_count += count;
491 buf_set(&in, &in, count);
492 s->resample_in_constraint= 0;
493 if(s->in_buffer_count != count || in_count)
499 s->resample_in_constraint= !!out_count;