00001 /* 00002 * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at) 00003 * 00004 * This file is part of libswresample 00005 * 00006 * libswresample is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2.1 of the License, or (at your option) any later version. 00010 * 00011 * libswresample is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with libswresample; if not, write to the Free Software 00018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00019 */ 00020 00021 #include "libavutil/opt.h" 00022 #include "swresample_internal.h" 00023 #include "audioconvert.h" 00024 #include "libavutil/avassert.h" 00025 #include "libavutil/audioconvert.h" 00026 00027 #define C30DB M_SQRT2 00028 #define C15DB 1.189207115 00029 #define C__0DB 1.0 00030 #define C_15DB 0.840896415 00031 #define C_30DB M_SQRT1_2 00032 #define C_45DB 0.594603558 00033 #define C_60DB 0.5 00034 00035 00036 //TODO split options array out? 00037 #define OFFSET(x) offsetof(SwrContext,x) 00038 static const AVOption options[]={ 00039 {"ich", "input channel count", OFFSET( in.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0}, 00040 {"och", "output channel count", OFFSET(out.ch_count ), AV_OPT_TYPE_INT, {.dbl=2}, 0, SWR_CH_MAX, 0}, 00041 {"uch", "used channel count", OFFSET(used_ch_count ), AV_OPT_TYPE_INT, {.dbl=0}, 0, SWR_CH_MAX, 0}, 00042 {"isr", "input sample rate" , OFFSET( in_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0}, 00043 {"osr", "output sample rate" , OFFSET(out_sample_rate), AV_OPT_TYPE_INT, {.dbl=48000}, 1, INT_MAX, 0}, 00044 //{"ip" , "input planar" , OFFSET( in.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0}, 00045 //{"op" , "output planar" , OFFSET(out.planar ), AV_OPT_TYPE_INT, {.dbl=0}, 0, 1, 0}, 00046 {"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}, 00047 {"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}, 00048 {"tsf", "internal sample format", OFFSET(int_sample_fmt ), AV_OPT_TYPE_INT, {.dbl=AV_SAMPLE_FMT_NONE}, -1, AV_SAMPLE_FMT_FLT, 0}, 00049 {"icl", "input channel layout" , OFFSET( in_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"}, 00050 {"ocl", "output channel layout", OFFSET(out_ch_layout), AV_OPT_TYPE_INT64, {.dbl=0}, 0, INT64_MAX, 0, "channel_layout"}, 00051 {"clev", "center mix level" , OFFSET(clev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0}, 00052 {"slev", "sourround mix level" , OFFSET(slev) , AV_OPT_TYPE_FLOAT, {.dbl=C_30DB}, 0, 4, 0}, 00053 {"rmvol", "rematrix volume" , OFFSET(rematrix_volume), AV_OPT_TYPE_FLOAT, {.dbl=1.0}, -1000, 1000, 0}, 00054 {"flags", NULL , OFFSET(flags) , AV_OPT_TYPE_FLAGS, {.dbl=0}, 0, UINT_MAX, 0, "flags"}, 00055 {"res", "force resampling", 0, AV_OPT_TYPE_CONST, {.dbl=SWR_FLAG_RESAMPLE}, INT_MIN, INT_MAX, 0, "flags"}, 00056 00057 {0} 00058 }; 00059 00060 static const char* context_to_name(void* ptr) { 00061 return "SWR"; 00062 } 00063 00064 static const AVClass av_class = { 00065 .class_name = "SwrContext", 00066 .item_name = context_to_name, 00067 .option = options, 00068 .version = LIBAVUTIL_VERSION_INT, 00069 .log_level_offset_offset = OFFSET(log_level_offset), 00070 .parent_log_context_offset = OFFSET(log_ctx), 00071 }; 00072 00073 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map){ 00074 if(!s || s->in_convert) // s needs to be allocated but not initialized 00075 return AVERROR(EINVAL); 00076 s->channel_map = channel_map; 00077 return 0; 00078 } 00079 00080 struct SwrContext *swr_alloc(void){ 00081 SwrContext *s= av_mallocz(sizeof(SwrContext)); 00082 if(s){ 00083 s->av_class= &av_class; 00084 av_opt_set_defaults(s); 00085 } 00086 return s; 00087 } 00088 00089 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s, 00090 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, 00091 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, 00092 int log_offset, void *log_ctx){ 00093 if(!s) s= swr_alloc(); 00094 if(!s) return NULL; 00095 00096 s->log_level_offset= log_offset; 00097 s->log_ctx= log_ctx; 00098 00099 av_opt_set_int(s, "ocl", out_ch_layout, 0); 00100 av_opt_set_int(s, "osf", out_sample_fmt, 0); 00101 av_opt_set_int(s, "osr", out_sample_rate, 0); 00102 av_opt_set_int(s, "icl", in_ch_layout, 0); 00103 av_opt_set_int(s, "isf", in_sample_fmt, 0); 00104 av_opt_set_int(s, "isr", in_sample_rate, 0); 00105 av_opt_set_int(s, "tsf", AV_SAMPLE_FMT_S16, 0); 00106 av_opt_set_int(s, "ich", av_get_channel_layout_nb_channels(s-> in_ch_layout), 0); 00107 av_opt_set_int(s, "och", av_get_channel_layout_nb_channels(s->out_ch_layout), 0); 00108 av_opt_set_int(s, "uch", 0, 0); 00109 return s; 00110 } 00111 00112 00113 static void free_temp(AudioData *a){ 00114 av_free(a->data); 00115 memset(a, 0, sizeof(*a)); 00116 } 00117 00118 void swr_free(SwrContext **ss){ 00119 SwrContext *s= *ss; 00120 if(s){ 00121 free_temp(&s->postin); 00122 free_temp(&s->midbuf); 00123 free_temp(&s->preout); 00124 free_temp(&s->in_buffer); 00125 swri_audio_convert_free(&s-> in_convert); 00126 swri_audio_convert_free(&s->out_convert); 00127 swri_audio_convert_free(&s->full_convert); 00128 swri_resample_free(&s->resample); 00129 } 00130 00131 av_freep(ss); 00132 } 00133 00134 int swr_init(struct SwrContext *s){ 00135 s->in_buffer_index= 0; 00136 s->in_buffer_count= 0; 00137 s->resample_in_constraint= 0; 00138 free_temp(&s->postin); 00139 free_temp(&s->midbuf); 00140 free_temp(&s->preout); 00141 free_temp(&s->in_buffer); 00142 swri_audio_convert_free(&s-> in_convert); 00143 swri_audio_convert_free(&s->out_convert); 00144 swri_audio_convert_free(&s->full_convert); 00145 00146 s-> in.planar= av_sample_fmt_is_planar(s-> in_sample_fmt); 00147 s->out.planar= av_sample_fmt_is_planar(s->out_sample_fmt); 00148 s-> in_sample_fmt= av_get_alt_sample_fmt(s-> in_sample_fmt, 0); 00149 s->out_sample_fmt= av_get_alt_sample_fmt(s->out_sample_fmt, 0); 00150 00151 if(s-> in_sample_fmt >= AV_SAMPLE_FMT_NB){ 00152 av_log(s, AV_LOG_ERROR, "Requested input sample format %d is invalid\n", s->in_sample_fmt); 00153 return AVERROR(EINVAL); 00154 } 00155 if(s->out_sample_fmt >= AV_SAMPLE_FMT_NB){ 00156 av_log(s, AV_LOG_ERROR, "Requested output sample format %d is invalid\n", s->out_sample_fmt); 00157 return AVERROR(EINVAL); 00158 } 00159 00160 if( s->int_sample_fmt != AV_SAMPLE_FMT_S16 00161 &&s->int_sample_fmt != AV_SAMPLE_FMT_FLT){ 00162 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)); 00163 return AVERROR(EINVAL); 00164 } 00165 00166 //FIXME should we allow/support using FLT on material that doesnt need it ? 00167 if(s->in_sample_fmt <= AV_SAMPLE_FMT_S16 || s->int_sample_fmt==AV_SAMPLE_FMT_S16){ 00168 s->int_sample_fmt= AV_SAMPLE_FMT_S16; 00169 }else 00170 s->int_sample_fmt= AV_SAMPLE_FMT_FLT; 00171 00172 00173 if (s->out_sample_rate!=s->in_sample_rate || (s->flags & SWR_FLAG_RESAMPLE)){ 00174 s->resample = swri_resample_init(s->resample, s->out_sample_rate, s->in_sample_rate, 16, 10, 0, 0.8); 00175 }else 00176 swri_resample_free(&s->resample); 00177 if(s->int_sample_fmt != AV_SAMPLE_FMT_S16 && s->resample){ 00178 av_log(s, AV_LOG_ERROR, "Resampling only supported with internal s16 currently\n"); //FIXME 00179 return -1; 00180 } 00181 00182 if(!s->used_ch_count) 00183 s->used_ch_count= s->in.ch_count; 00184 00185 if(s->used_ch_count && s-> in_ch_layout && s->used_ch_count != av_get_channel_layout_nb_channels(s-> in_ch_layout)){ 00186 av_log(s, AV_LOG_WARNING, "Input channel layout has a different number of channels than the number of used channels, ignoring layout\n"); 00187 s-> in_ch_layout= 0; 00188 } 00189 00190 if(!s-> in_ch_layout) 00191 s-> in_ch_layout= av_get_default_channel_layout(s->used_ch_count); 00192 if(!s->out_ch_layout) 00193 s->out_ch_layout= av_get_default_channel_layout(s->out.ch_count); 00194 00195 s->rematrix= s->out_ch_layout !=s->in_ch_layout || s->rematrix_volume!=1.0; 00196 00197 #define RSC 1 //FIXME finetune 00198 if(!s-> in.ch_count) 00199 s-> in.ch_count= av_get_channel_layout_nb_channels(s-> in_ch_layout); 00200 if(!s->used_ch_count) 00201 s->used_ch_count= s->in.ch_count; 00202 if(!s->out.ch_count) 00203 s->out.ch_count= av_get_channel_layout_nb_channels(s->out_ch_layout); 00204 00205 if(!s-> in.ch_count){ 00206 av_assert0(!s->in_ch_layout); 00207 av_log(s, AV_LOG_ERROR, "Input channel count and layout are unset\n"); 00208 return -1; 00209 } 00210 00211 av_assert0(s->used_ch_count); 00212 av_assert0(s->out.ch_count); 00213 s->resample_first= RSC*s->out.ch_count/s->in.ch_count - RSC < s->out_sample_rate/(float)s-> in_sample_rate - 1.0; 00214 00215 s-> in.bps= av_get_bytes_per_sample(s-> in_sample_fmt); 00216 s->int_bps= av_get_bytes_per_sample(s->int_sample_fmt); 00217 s->out.bps= av_get_bytes_per_sample(s->out_sample_fmt); 00218 00219 if(!s->resample && !s->rematrix && !s->channel_map){ 00220 s->full_convert = swri_audio_convert_alloc(s->out_sample_fmt, 00221 s-> in_sample_fmt, s-> in.ch_count, NULL, 0); 00222 return 0; 00223 } 00224 00225 s->in_convert = swri_audio_convert_alloc(s->int_sample_fmt, 00226 s-> in_sample_fmt, s->used_ch_count, s->channel_map, 0); 00227 s->out_convert= swri_audio_convert_alloc(s->out_sample_fmt, 00228 s->int_sample_fmt, s->out.ch_count, NULL, 0); 00229 00230 00231 s->postin= s->in; 00232 s->preout= s->out; 00233 s->midbuf= s->in; 00234 s->in_buffer= s->in; 00235 if(s->channel_map){ 00236 s->postin.ch_count= 00237 s->midbuf.ch_count= 00238 s->in_buffer.ch_count= s->used_ch_count; 00239 } 00240 if(!s->resample_first){ 00241 s->midbuf.ch_count= s->out.ch_count; 00242 s->in_buffer.ch_count = s->out.ch_count; 00243 } 00244 00245 s->in_buffer.bps = s->postin.bps = s->midbuf.bps = s->preout.bps = s->int_bps; 00246 s->in_buffer.planar = s->postin.planar = s->midbuf.planar = s->preout.planar = 1; 00247 00248 00249 if(s->rematrix) 00250 return swri_rematrix_init(s); 00251 00252 return 0; 00253 } 00254 00255 static int realloc_audio(AudioData *a, int count){ 00256 int i, countb; 00257 AudioData old; 00258 00259 if(a->count >= count) 00260 return 0; 00261 00262 count*=2; 00263 00264 countb= FFALIGN(count*a->bps, 32); 00265 old= *a; 00266 00267 av_assert0(a->planar); 00268 av_assert0(a->bps); 00269 av_assert0(a->ch_count); 00270 00271 a->data= av_malloc(countb*a->ch_count); 00272 if(!a->data) 00273 return AVERROR(ENOMEM); 00274 for(i=0; i<a->ch_count; i++){ 00275 a->ch[i]= a->data + i*(a->planar ? countb : a->bps); 00276 if(a->planar) memcpy(a->ch[i], old.ch[i], a->count*a->bps); 00277 } 00278 av_free(old.data); 00279 a->count= count; 00280 00281 return 1; 00282 } 00283 00284 static void copy(AudioData *out, AudioData *in, 00285 int count){ 00286 av_assert0(out->planar == in->planar); 00287 av_assert0(out->bps == in->bps); 00288 av_assert0(out->ch_count == in->ch_count); 00289 if(out->planar){ 00290 int ch; 00291 for(ch=0; ch<out->ch_count; ch++) 00292 memcpy(out->ch[ch], in->ch[ch], count*out->bps); 00293 }else 00294 memcpy(out->ch[0], in->ch[0], count*out->ch_count*out->bps); 00295 } 00296 00297 static void fill_audiodata(AudioData *out, uint8_t *in_arg [SWR_CH_MAX]){ 00298 int i; 00299 if(out->planar){ 00300 for(i=0; i<out->ch_count; i++) 00301 out->ch[i]= in_arg[i]; 00302 }else{ 00303 for(i=0; i<out->ch_count; i++) 00304 out->ch[i]= in_arg[0] + i*out->bps; 00305 } 00306 } 00307 00312 static void buf_set(AudioData *out, AudioData *in, int count){ 00313 if(in->planar){ 00314 int ch; 00315 for(ch=0; ch<out->ch_count; ch++) 00316 out->ch[ch]= in->ch[ch] + count*out->bps; 00317 }else 00318 out->ch[0]= in->ch[0] + count*out->ch_count*out->bps; 00319 } 00320 00325 static int resample(SwrContext *s, AudioData *out_param, int out_count, 00326 const AudioData * in_param, int in_count){ 00327 AudioData in, out, tmp; 00328 int ret_sum=0; 00329 int border=0; 00330 00331 tmp=out=*out_param; 00332 in = *in_param; 00333 00334 do{ 00335 int ret, size, consumed; 00336 if(!s->resample_in_constraint && s->in_buffer_count){ 00337 buf_set(&tmp, &s->in_buffer, s->in_buffer_index); 00338 ret= swri_multiple_resample(s->resample, &out, out_count, &tmp, s->in_buffer_count, &consumed); 00339 out_count -= ret; 00340 ret_sum += ret; 00341 buf_set(&out, &out, ret); 00342 s->in_buffer_count -= consumed; 00343 s->in_buffer_index += consumed; 00344 00345 if(!in_count) 00346 break; 00347 if(s->in_buffer_count <= border){ 00348 buf_set(&in, &in, -s->in_buffer_count); 00349 in_count += s->in_buffer_count; 00350 s->in_buffer_count=0; 00351 s->in_buffer_index=0; 00352 border = 0; 00353 } 00354 } 00355 00356 if(in_count && !s->in_buffer_count){ 00357 s->in_buffer_index=0; 00358 ret= swri_multiple_resample(s->resample, &out, out_count, &in, in_count, &consumed); 00359 out_count -= ret; 00360 ret_sum += ret; 00361 buf_set(&out, &out, ret); 00362 in_count -= consumed; 00363 buf_set(&in, &in, consumed); 00364 } 00365 00366 //TODO is this check sane considering the advanced copy avoidance below 00367 size= s->in_buffer_index + s->in_buffer_count + in_count; 00368 if( size > s->in_buffer.count 00369 && s->in_buffer_count + in_count <= s->in_buffer_index){ 00370 buf_set(&tmp, &s->in_buffer, s->in_buffer_index); 00371 copy(&s->in_buffer, &tmp, s->in_buffer_count); 00372 s->in_buffer_index=0; 00373 }else 00374 if((ret=realloc_audio(&s->in_buffer, size)) < 0) 00375 return ret; 00376 00377 if(in_count){ 00378 int count= in_count; 00379 if(s->in_buffer_count && s->in_buffer_count+2 < count && out_count) count= s->in_buffer_count+2; 00380 00381 buf_set(&tmp, &s->in_buffer, s->in_buffer_index + s->in_buffer_count); 00382 copy(&tmp, &in, /*in_*/count); 00383 s->in_buffer_count += count; 00384 in_count -= count; 00385 border += count; 00386 buf_set(&in, &in, count); 00387 s->resample_in_constraint= 0; 00388 if(s->in_buffer_count != count || in_count) 00389 continue; 00390 } 00391 break; 00392 }while(1); 00393 00394 s->resample_in_constraint= !!out_count; 00395 00396 return ret_sum; 00397 } 00398 00399 int swr_convert(struct SwrContext *s, uint8_t *out_arg[SWR_CH_MAX], int out_count, 00400 const uint8_t *in_arg [SWR_CH_MAX], int in_count){ 00401 AudioData *postin, *midbuf, *preout; 00402 int ret/*, in_max*/; 00403 AudioData * in= &s->in; 00404 AudioData *out= &s->out; 00405 AudioData preout_tmp, midbuf_tmp; 00406 00407 if(!s->resample){ 00408 if(in_count > out_count) 00409 return -1; 00410 out_count = in_count; 00411 } 00412 00413 if(!in_arg){ 00414 if(s->in_buffer_count){ 00415 AudioData *a= &s->in_buffer; 00416 int i, j, ret; 00417 if((ret=realloc_audio(a, s->in_buffer_index + 2*s->in_buffer_count)) < 0) 00418 return ret; 00419 av_assert0(a->planar); 00420 for(i=0; i<a->ch_count; i++){ 00421 for(j=0; j<s->in_buffer_count; j++){ 00422 memcpy(a->ch[i] + (s->in_buffer_index+s->in_buffer_count+j )*a->bps, 00423 a->ch[i] + (s->in_buffer_index+s->in_buffer_count-j-1)*a->bps, a->bps); 00424 } 00425 } 00426 s->in_buffer_count += (s->in_buffer_count+1)/2; 00427 s->resample_in_constraint = 0; 00428 }else{ 00429 return 0; 00430 } 00431 }else 00432 fill_audiodata(in , (void*)in_arg); 00433 fill_audiodata(out, out_arg); 00434 00435 if(s->full_convert){ 00436 av_assert0(!s->resample); 00437 swri_audio_convert(s->full_convert, out, in, in_count); 00438 return out_count; 00439 } 00440 00441 // in_max= out_count*(int64_t)s->in_sample_rate / s->out_sample_rate + resample_filter_taps; 00442 // in_count= FFMIN(in_count, in_in + 2 - s->hist_buffer_count); 00443 00444 if((ret=realloc_audio(&s->postin, in_count))<0) 00445 return ret; 00446 if(s->resample_first){ 00447 av_assert0(s->midbuf.ch_count == s->used_ch_count); 00448 if((ret=realloc_audio(&s->midbuf, out_count))<0) 00449 return ret; 00450 }else{ 00451 av_assert0(s->midbuf.ch_count == s->out.ch_count); 00452 if((ret=realloc_audio(&s->midbuf, in_count))<0) 00453 return ret; 00454 } 00455 if((ret=realloc_audio(&s->preout, out_count))<0) 00456 return ret; 00457 00458 postin= &s->postin; 00459 00460 midbuf_tmp= s->midbuf; 00461 midbuf= &midbuf_tmp; 00462 preout_tmp= s->preout; 00463 preout= &preout_tmp; 00464 00465 if(s->int_sample_fmt == s-> in_sample_fmt && s->in.planar) 00466 postin= in; 00467 00468 if(s->resample_first ? !s->resample : !s->rematrix) 00469 midbuf= postin; 00470 00471 if(s->resample_first ? !s->rematrix : !s->resample) 00472 preout= midbuf; 00473 00474 if(s->int_sample_fmt == s->out_sample_fmt && s->out.planar){ 00475 if(preout==in){ 00476 out_count= FFMIN(out_count, in_count); //TODO check at teh end if this is needed or redundant 00477 av_assert0(s->in.planar); //we only support planar internally so it has to be, we support copying non planar though 00478 copy(out, in, out_count); 00479 return out_count; 00480 } 00481 else if(preout==postin) preout= midbuf= postin= out; 00482 else if(preout==midbuf) preout= midbuf= out; 00483 else preout= out; 00484 } 00485 00486 if(in != postin){ 00487 swri_audio_convert(s->in_convert, postin, in, in_count); 00488 } 00489 00490 if(s->resample_first){ 00491 if(postin != midbuf) 00492 out_count= resample(s, midbuf, out_count, postin, in_count); 00493 if(midbuf != preout) 00494 swri_rematrix(s, preout, midbuf, out_count, preout==out); 00495 }else{ 00496 if(postin != midbuf) 00497 swri_rematrix(s, midbuf, postin, in_count, midbuf==out); 00498 if(midbuf != preout) 00499 out_count= resample(s, preout, out_count, midbuf, in_count); 00500 } 00501 00502 if(preout != out){ 00503 //FIXME packed doesnt need more than 1 chan here! 00504 swri_audio_convert(s->out_convert, out, preout, out_count); 00505 } 00506 if(!in_arg) 00507 s->in_buffer_count = 0; 00508 return out_count; 00509 } 00510